AOMedia AV1 Codec
aomenc
1/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12#include "apps/aomenc.h"
13
14#include "config/aom_config.h"
15
16#include <assert.h>
17#include <limits.h>
18#include <math.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#if CONFIG_AV1_DECODER
25#include "aom/aom_decoder.h"
26#include "aom/aomdx.h"
27#endif
28
29#include "aom/aom_encoder.h"
30#include "aom/aom_integer.h"
31#include "aom/aomcx.h"
32#include "aom_dsp/aom_dsp_common.h"
33#include "aom_ports/aom_timer.h"
34#include "aom_ports/mem_ops.h"
35#include "common/args.h"
36#include "common/ivfenc.h"
37#include "common/tools_common.h"
38#include "common/warnings.h"
39
40#if CONFIG_WEBM_IO
41#include "common/webmenc.h"
42#endif
43
44#include "common/y4minput.h"
45#include "examples/encoder_util.h"
46#include "stats/aomstats.h"
47#include "stats/rate_hist.h"
48
49#if CONFIG_LIBYUV
50#include "third_party/libyuv/include/libyuv/scale.h"
51#endif
52
53/* Swallow warnings about unused results of fread/fwrite */
54static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
55 return fread(ptr, size, nmemb, stream);
56}
57#define fread wrap_fread
58
59static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
60 FILE *stream) {
61 return fwrite(ptr, size, nmemb, stream);
62}
63#define fwrite wrap_fwrite
64
65static const char *exec_name;
66
67static AOM_TOOLS_FORMAT_PRINTF(3, 0) void warn_or_exit_on_errorv(
68 aom_codec_ctx_t *ctx, int fatal, const char *s, va_list ap) {
69 if (ctx->err) {
70 const char *detail = aom_codec_error_detail(ctx);
71
72 vfprintf(stderr, s, ap);
73 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
74
75 if (detail) fprintf(stderr, " %s\n", detail);
76
77 if (fatal) {
78 aom_codec_destroy(ctx);
79 exit(EXIT_FAILURE);
80 }
81 }
82}
83
84static AOM_TOOLS_FORMAT_PRINTF(2,
85 3) void ctx_exit_on_error(aom_codec_ctx_t *ctx,
86 const char *s, ...) {
87 va_list ap;
88
89 va_start(ap, s);
90 warn_or_exit_on_errorv(ctx, 1, s, ap);
91 va_end(ap);
92}
93
94static AOM_TOOLS_FORMAT_PRINTF(3, 4) void warn_or_exit_on_error(
95 aom_codec_ctx_t *ctx, int fatal, const char *s, ...) {
96 va_list ap;
97
98 va_start(ap, s);
99 warn_or_exit_on_errorv(ctx, fatal, s, ap);
100 va_end(ap);
101}
102
103static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
104 FILE *f = input_ctx->file;
105 y4m_input *y4m = &input_ctx->y4m;
106 int shortread = 0;
107
108 if (input_ctx->file_type == FILE_TYPE_Y4M) {
109 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
110 } else {
111 shortread = read_yuv_frame(input_ctx, img);
112 }
113
114 return !shortread;
115}
116
117static int file_is_y4m(const char detect[4]) {
118 if (memcmp(detect, "YUV4", 4) == 0) {
119 return 1;
120 }
121 return 0;
122}
123
124static int fourcc_is_ivf(const char detect[4]) {
125 if (memcmp(detect, "DKIF", 4) == 0) {
126 return 1;
127 }
128 return 0;
129}
130
131static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
220#if CONFIG_DENOISE
223 AV1E_SET_ENABLE_DNL_DENOISING,
224#endif // CONFIG_DENOISE
234#if CONFIG_TUNE_VMAF
236#endif
246 0 };
247
248static const arg_def_t *const main_args[] = {
249 &g_av1_codec_arg_defs.help,
250 &g_av1_codec_arg_defs.use_cfg,
251 &g_av1_codec_arg_defs.debugmode,
252 &g_av1_codec_arg_defs.outputfile,
253 &g_av1_codec_arg_defs.codecarg,
254 &g_av1_codec_arg_defs.passes,
255 &g_av1_codec_arg_defs.pass_arg,
256 &g_av1_codec_arg_defs.fpf_name,
257 &g_av1_codec_arg_defs.limit,
258 &g_av1_codec_arg_defs.skip,
259 &g_av1_codec_arg_defs.good_dl,
260 &g_av1_codec_arg_defs.rt_dl,
261 &g_av1_codec_arg_defs.ai_dl,
262 &g_av1_codec_arg_defs.quietarg,
263 &g_av1_codec_arg_defs.verbosearg,
264 &g_av1_codec_arg_defs.psnrarg,
265 &g_av1_codec_arg_defs.use_webm,
266 &g_av1_codec_arg_defs.use_ivf,
267 &g_av1_codec_arg_defs.use_obu,
268 &g_av1_codec_arg_defs.q_hist_n,
269 &g_av1_codec_arg_defs.rate_hist_n,
270 &g_av1_codec_arg_defs.disable_warnings,
271 &g_av1_codec_arg_defs.disable_warning_prompt,
272 &g_av1_codec_arg_defs.recontest,
273 NULL
274};
275
276static const arg_def_t *const global_args[] = {
277 &g_av1_codec_arg_defs.use_nv12,
278 &g_av1_codec_arg_defs.use_yv12,
279 &g_av1_codec_arg_defs.use_i420,
280 &g_av1_codec_arg_defs.use_i422,
281 &g_av1_codec_arg_defs.use_i444,
282 &g_av1_codec_arg_defs.usage,
283 &g_av1_codec_arg_defs.threads,
284 &g_av1_codec_arg_defs.profile,
285 &g_av1_codec_arg_defs.width,
286 &g_av1_codec_arg_defs.height,
287 &g_av1_codec_arg_defs.forced_max_frame_width,
288 &g_av1_codec_arg_defs.forced_max_frame_height,
289#if CONFIG_WEBM_IO
290 &g_av1_codec_arg_defs.stereo_mode,
291#endif
292 &g_av1_codec_arg_defs.timebase,
293 &g_av1_codec_arg_defs.framerate,
294 &g_av1_codec_arg_defs.global_error_resilient,
295 &g_av1_codec_arg_defs.bitdeptharg,
296 &g_av1_codec_arg_defs.inbitdeptharg,
297 &g_av1_codec_arg_defs.lag_in_frames,
298 &g_av1_codec_arg_defs.large_scale_tile,
299 &g_av1_codec_arg_defs.monochrome,
300 &g_av1_codec_arg_defs.full_still_picture_hdr,
301 &g_av1_codec_arg_defs.use_16bit_internal,
302 &g_av1_codec_arg_defs.save_as_annexb,
303 NULL
304};
305
306static const arg_def_t *const rc_args[] = {
307 &g_av1_codec_arg_defs.dropframe_thresh,
308 &g_av1_codec_arg_defs.resize_mode,
309 &g_av1_codec_arg_defs.resize_denominator,
310 &g_av1_codec_arg_defs.resize_kf_denominator,
311 &g_av1_codec_arg_defs.superres_mode,
312 &g_av1_codec_arg_defs.superres_denominator,
313 &g_av1_codec_arg_defs.superres_kf_denominator,
314 &g_av1_codec_arg_defs.superres_qthresh,
315 &g_av1_codec_arg_defs.superres_kf_qthresh,
316 &g_av1_codec_arg_defs.end_usage,
317 &g_av1_codec_arg_defs.target_bitrate,
318 &g_av1_codec_arg_defs.min_quantizer,
319 &g_av1_codec_arg_defs.max_quantizer,
320 &g_av1_codec_arg_defs.undershoot_pct,
321 &g_av1_codec_arg_defs.overshoot_pct,
322 &g_av1_codec_arg_defs.buf_sz,
323 &g_av1_codec_arg_defs.buf_initial_sz,
324 &g_av1_codec_arg_defs.buf_optimal_sz,
325 &g_av1_codec_arg_defs.bias_pct,
326 &g_av1_codec_arg_defs.minsection_pct,
327 &g_av1_codec_arg_defs.maxsection_pct,
328 NULL
329};
330
331static const arg_def_t *const kf_args[] = {
332 &g_av1_codec_arg_defs.fwd_kf_enabled,
333 &g_av1_codec_arg_defs.kf_min_dist,
334 &g_av1_codec_arg_defs.kf_max_dist,
335 &g_av1_codec_arg_defs.kf_disabled,
336 &g_av1_codec_arg_defs.sframe_dist,
337 &g_av1_codec_arg_defs.sframe_mode,
338 NULL
339};
340
341// TODO(bohanli): Currently all options are supported by the key & value API.
342// Consider removing the control ID usages?
343static const arg_def_t *const av1_ctrl_args[] = {
344 &g_av1_codec_arg_defs.cpu_used_av1,
345 &g_av1_codec_arg_defs.auto_altref,
346 &g_av1_codec_arg_defs.sharpness,
347 &g_av1_codec_arg_defs.static_thresh,
348 &g_av1_codec_arg_defs.rowmtarg,
349 &g_av1_codec_arg_defs.fpmtarg,
350 &g_av1_codec_arg_defs.tile_cols,
351 &g_av1_codec_arg_defs.tile_rows,
352 &g_av1_codec_arg_defs.enable_tpl_model,
353 &g_av1_codec_arg_defs.enable_keyframe_filtering,
354 &g_av1_codec_arg_defs.arnr_maxframes,
355 &g_av1_codec_arg_defs.arnr_strength,
356 &g_av1_codec_arg_defs.tune_metric,
357 &g_av1_codec_arg_defs.cq_level,
358 &g_av1_codec_arg_defs.max_intra_rate_pct,
359 &g_av1_codec_arg_defs.max_inter_rate_pct,
360 &g_av1_codec_arg_defs.gf_cbr_boost_pct,
361 &g_av1_codec_arg_defs.lossless,
362 &g_av1_codec_arg_defs.enable_cdef,
363 &g_av1_codec_arg_defs.enable_restoration,
364 &g_av1_codec_arg_defs.enable_rect_partitions,
365 &g_av1_codec_arg_defs.enable_ab_partitions,
366 &g_av1_codec_arg_defs.enable_1to4_partitions,
367 &g_av1_codec_arg_defs.min_partition_size,
368 &g_av1_codec_arg_defs.max_partition_size,
369 &g_av1_codec_arg_defs.enable_dual_filter,
370 &g_av1_codec_arg_defs.enable_chroma_deltaq,
371 &g_av1_codec_arg_defs.enable_intra_edge_filter,
372 &g_av1_codec_arg_defs.enable_order_hint,
373 &g_av1_codec_arg_defs.enable_tx64,
374 &g_av1_codec_arg_defs.enable_flip_idtx,
375 &g_av1_codec_arg_defs.enable_rect_tx,
376 &g_av1_codec_arg_defs.enable_dist_wtd_comp,
377 &g_av1_codec_arg_defs.enable_masked_comp,
378 &g_av1_codec_arg_defs.enable_onesided_comp,
379 &g_av1_codec_arg_defs.enable_interintra_comp,
380 &g_av1_codec_arg_defs.enable_smooth_interintra,
381 &g_av1_codec_arg_defs.enable_diff_wtd_comp,
382 &g_av1_codec_arg_defs.enable_interinter_wedge,
383 &g_av1_codec_arg_defs.enable_interintra_wedge,
384 &g_av1_codec_arg_defs.enable_global_motion,
385 &g_av1_codec_arg_defs.enable_warped_motion,
386 &g_av1_codec_arg_defs.enable_filter_intra,
387 &g_av1_codec_arg_defs.enable_smooth_intra,
388 &g_av1_codec_arg_defs.enable_paeth_intra,
389 &g_av1_codec_arg_defs.enable_cfl_intra,
390 &g_av1_codec_arg_defs.enable_diagonal_intra,
391 &g_av1_codec_arg_defs.force_video_mode,
392 &g_av1_codec_arg_defs.enable_obmc,
393 &g_av1_codec_arg_defs.enable_overlay,
394 &g_av1_codec_arg_defs.enable_palette,
395 &g_av1_codec_arg_defs.enable_intrabc,
396 &g_av1_codec_arg_defs.enable_angle_delta,
397 &g_av1_codec_arg_defs.disable_trellis_quant,
398 &g_av1_codec_arg_defs.enable_qm,
399 &g_av1_codec_arg_defs.qm_min,
400 &g_av1_codec_arg_defs.qm_max,
401 &g_av1_codec_arg_defs.reduced_tx_type_set,
402 &g_av1_codec_arg_defs.use_intra_dct_only,
403 &g_av1_codec_arg_defs.use_inter_dct_only,
404 &g_av1_codec_arg_defs.use_intra_default_tx_only,
405 &g_av1_codec_arg_defs.quant_b_adapt,
406 &g_av1_codec_arg_defs.coeff_cost_upd_freq,
407 &g_av1_codec_arg_defs.mode_cost_upd_freq,
408 &g_av1_codec_arg_defs.mv_cost_upd_freq,
409 &g_av1_codec_arg_defs.frame_parallel_decoding,
410 &g_av1_codec_arg_defs.error_resilient_mode,
411 &g_av1_codec_arg_defs.aq_mode,
412 &g_av1_codec_arg_defs.deltaq_mode,
413 &g_av1_codec_arg_defs.deltaq_strength,
414 &g_av1_codec_arg_defs.deltalf_mode,
415 &g_av1_codec_arg_defs.frame_periodic_boost,
416 &g_av1_codec_arg_defs.noise_sens,
417 &g_av1_codec_arg_defs.tune_content,
418 &g_av1_codec_arg_defs.cdf_update_mode,
419 &g_av1_codec_arg_defs.input_color_primaries,
420 &g_av1_codec_arg_defs.input_transfer_characteristics,
421 &g_av1_codec_arg_defs.input_matrix_coefficients,
422 &g_av1_codec_arg_defs.input_chroma_sample_position,
423 &g_av1_codec_arg_defs.min_gf_interval,
424 &g_av1_codec_arg_defs.max_gf_interval,
425 &g_av1_codec_arg_defs.gf_min_pyr_height,
426 &g_av1_codec_arg_defs.gf_max_pyr_height,
427 &g_av1_codec_arg_defs.superblock_size,
428 &g_av1_codec_arg_defs.num_tg,
429 &g_av1_codec_arg_defs.mtu_size,
430 &g_av1_codec_arg_defs.timing_info,
431 &g_av1_codec_arg_defs.film_grain_test,
432 &g_av1_codec_arg_defs.film_grain_table,
433#if CONFIG_DENOISE
434 &g_av1_codec_arg_defs.denoise_noise_level,
435 &g_av1_codec_arg_defs.denoise_block_size,
436 &g_av1_codec_arg_defs.enable_dnl_denoising,
437#endif // CONFIG_DENOISE
438 &g_av1_codec_arg_defs.max_reference_frames,
439 &g_av1_codec_arg_defs.reduced_reference_set,
440 &g_av1_codec_arg_defs.enable_ref_frame_mvs,
441 &g_av1_codec_arg_defs.target_seq_level_idx,
442 &g_av1_codec_arg_defs.set_tier_mask,
443 &g_av1_codec_arg_defs.set_min_cr,
444 &g_av1_codec_arg_defs.vbr_corpus_complexity_lap,
445 &g_av1_codec_arg_defs.input_chroma_subsampling_x,
446 &g_av1_codec_arg_defs.input_chroma_subsampling_y,
447#if CONFIG_TUNE_VMAF
448 &g_av1_codec_arg_defs.vmaf_model_path,
449#endif
450 &g_av1_codec_arg_defs.dv_cost_upd_freq,
451 &g_av1_codec_arg_defs.partition_info_path,
452 &g_av1_codec_arg_defs.enable_directional_intra,
453 &g_av1_codec_arg_defs.enable_tx_size_search,
454 &g_av1_codec_arg_defs.loopfilter_control,
455 &g_av1_codec_arg_defs.auto_intra_tools_off,
456 &g_av1_codec_arg_defs.enable_rate_guide_deltaq,
457 &g_av1_codec_arg_defs.rate_distribution_info,
458 &g_av1_codec_arg_defs.enable_low_complexity_decode,
459 NULL,
460};
461
462static const arg_def_t *const av1_key_val_args[] = {
463 &g_av1_codec_arg_defs.passes,
464 &g_av1_codec_arg_defs.two_pass_output,
465 &g_av1_codec_arg_defs.second_pass_log,
466 &g_av1_codec_arg_defs.fwd_kf_dist,
467 &g_av1_codec_arg_defs.strict_level_conformance,
468 &g_av1_codec_arg_defs.sb_qp_sweep,
469 &g_av1_codec_arg_defs.dist_metric,
470 &g_av1_codec_arg_defs.kf_max_pyr_height,
471 &g_av1_codec_arg_defs.auto_tiles,
472 NULL,
473};
474
475static const arg_def_t *const no_args[] = { NULL };
476
477static void show_help(FILE *fout, int shorthelp) {
478 fprintf(fout, "Usage: %s <options> -o dst_filename src_filename\n",
479 exec_name);
480
481 if (shorthelp) {
482 fprintf(fout, "Use --help to see the full list of options.\n");
483 return;
484 }
485
486 fprintf(fout, "\nOptions:\n");
487 arg_show_usage(fout, main_args);
488 fprintf(fout, "\nEncoder Global Options:\n");
489 arg_show_usage(fout, global_args);
490 fprintf(fout, "\nRate Control Options:\n");
491 arg_show_usage(fout, rc_args);
492 fprintf(fout, "\nKeyframe Placement Options:\n");
493 arg_show_usage(fout, kf_args);
494#if CONFIG_AV1_ENCODER
495 fprintf(fout, "\nAV1 Specific Options:\n");
496 arg_show_usage(fout, av1_ctrl_args);
497 arg_show_usage(fout, av1_key_val_args);
498#endif
499 fprintf(fout,
500 "\nStream timebase (--timebase):\n"
501 " The desired precision of timestamps in the output, expressed\n"
502 " in fractional seconds. Default is 1/1000.\n");
503 fprintf(fout, "\nIncluded encoders:\n\n");
504
505 const int num_encoder = get_aom_encoder_count();
506 for (int i = 0; i < num_encoder; ++i) {
507 aom_codec_iface_t *encoder = get_aom_encoder_by_index(i);
508 const char *defstr = (i == (num_encoder - 1)) ? "(default)" : "";
509 fprintf(fout, " %-6s - %s %s\n", get_short_name_by_aom_encoder(encoder),
510 aom_codec_iface_name(encoder), defstr);
511 }
512 fprintf(fout, "\n ");
513 fprintf(fout, "Use --codec to switch to a non-default encoder.\n\n");
514}
515
516void usage_exit(void) {
517 show_help(stderr, 1);
518 exit(EXIT_FAILURE);
519}
520
521#if CONFIG_AV1_ENCODER
522#define ARG_CTRL_CNT_MAX NELEMENTS(av1_arg_ctrl_map)
523#define ARG_KEY_VAL_CNT_MAX NELEMENTS(av1_key_val_args)
524#endif
525
526#if !CONFIG_WEBM_IO
527typedef int stereo_format_t;
528struct WebmOutputContext {
529 int debug;
530};
531#endif
532
533/* Per-stream configuration */
534struct stream_config {
535 struct aom_codec_enc_cfg cfg;
536 const char *out_fn;
537 const char *stats_fn;
538 stereo_format_t stereo_fmt;
539 int arg_ctrls[ARG_CTRL_CNT_MAX][2];
540 int arg_ctrl_cnt;
541 const char *arg_key_vals[ARG_KEY_VAL_CNT_MAX][2];
542 int arg_key_val_cnt;
543 int write_webm;
544 const char *film_grain_filename;
545 int write_ivf;
546 // whether to use 16bit internal buffers
547 int use_16bit_internal;
548#if CONFIG_TUNE_VMAF
549 const char *vmaf_model_path;
550#endif
551 const char *partition_info_path;
552 unsigned int enable_rate_guide_deltaq;
553 const char *rate_distribution_info;
554 aom_color_range_t color_range;
555 const char *two_pass_input;
556 const char *two_pass_output;
557 int two_pass_width;
558 int two_pass_height;
559 unsigned int enable_low_complexity_decode;
560};
561
562struct stream_state {
563 int index;
564 struct stream_state *next;
565 struct stream_config config;
566 FILE *file;
567 struct rate_hist *rate_hist;
568 struct WebmOutputContext webm_ctx;
569 uint64_t psnr_sse_total[2];
570 uint64_t psnr_samples_total[2];
571 double psnr_totals[2][4];
572 int psnr_count[2];
573 int counts[64];
574 aom_codec_ctx_t encoder;
575 unsigned int frames_out;
576 uint64_t cx_time;
577 size_t nbytes;
578 stats_io_t stats;
579 struct aom_image *img;
580 aom_codec_ctx_t decoder;
581 int mismatch_seen;
582 unsigned int chroma_subsampling_x;
583 unsigned int chroma_subsampling_y;
584 const char *orig_out_fn;
585 unsigned int orig_width;
586 unsigned int orig_height;
587 int orig_write_webm;
588 int orig_write_ivf;
589 char tmp_out_fn[1000];
590};
591
592static void validate_positive_rational(const char *msg,
593 struct aom_rational *rat) {
594 if (rat->den < 0) {
595 rat->num *= -1;
596 rat->den *= -1;
597 }
598
599 if (rat->num < 0) die("Error: %s must be positive\n", msg);
600
601 if (!rat->den) die("Error: %s has zero denominator\n", msg);
602}
603
604static void init_config(cfg_options_t *config) {
605 memset(config, 0, sizeof(cfg_options_t));
606 config->super_block_size = 0; // Dynamic
607 config->max_partition_size = 128;
608 config->min_partition_size = 4;
609 config->disable_trellis_quant = 3;
610}
611
612/* Parses global config arguments into the AvxEncoderConfig. Note that
613 * argv is modified and overwrites all parsed arguments.
614 */
615static void parse_global_config(struct AvxEncoderConfig *global, char ***argv) {
616 char **argi, **argj;
617 struct arg arg;
618 const int num_encoder = get_aom_encoder_count();
619 char **argv_local = (char **)*argv;
620 if (num_encoder < 1) die("Error: no valid encoder available\n");
621
622 /* Initialize default parameters */
623 memset(global, 0, sizeof(*global));
624 global->codec = get_aom_encoder_by_index(num_encoder - 1);
625 global->passes = 0;
626 global->color_type = I420;
627 global->csp = AOM_CSP_UNKNOWN;
628 global->show_psnr = 0;
629
630 int cfg_included = 0;
631 init_config(&global->encoder_config);
632
633 for (argi = argj = argv_local; (*argj = *argi); argi += arg.argv_step) {
634 arg.argv_step = 1;
635
636 if (arg_match(&arg, &g_av1_codec_arg_defs.use_cfg, argi)) {
637 if (!cfg_included) {
638 parse_cfg(arg.val, &global->encoder_config);
639 cfg_included = 1;
640 }
641 } else if (arg_match(&arg, &g_av1_codec_arg_defs.help, argi)) {
642 show_help(stdout, 0);
643 exit(EXIT_SUCCESS);
644 } else if (arg_match(&arg, &g_av1_codec_arg_defs.codecarg, argi)) {
645 global->codec = get_aom_encoder_by_short_name(arg.val);
646 if (!global->codec)
647 die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
648 } else if (arg_match(&arg, &g_av1_codec_arg_defs.passes, argi)) {
649 global->passes = arg_parse_uint(&arg);
650
651 if (global->passes < 1 || global->passes > 3)
652 die("Error: Invalid number of passes (%d)\n", global->passes);
653 } else if (arg_match(&arg, &g_av1_codec_arg_defs.pass_arg, argi)) {
654 global->pass = arg_parse_uint(&arg);
655
656 if (global->pass < 1 || global->pass > 3)
657 die("Error: Invalid pass selected (%d)\n", global->pass);
658 } else if (arg_match(&arg,
659 &g_av1_codec_arg_defs.input_chroma_sample_position,
660 argi)) {
661 global->csp = arg_parse_enum(&arg);
662 /* Flag is used by later code as well, preserve it. */
663 argj++;
664 } else if (arg_match(&arg, &g_av1_codec_arg_defs.usage, argi)) {
665 global->usage = arg_parse_uint(&arg);
666 } else if (arg_match(&arg, &g_av1_codec_arg_defs.good_dl, argi)) {
667 global->usage = AOM_USAGE_GOOD_QUALITY; // Good quality usage
668 } else if (arg_match(&arg, &g_av1_codec_arg_defs.rt_dl, argi)) {
669 global->usage = AOM_USAGE_REALTIME; // Real-time usage
670 } else if (arg_match(&arg, &g_av1_codec_arg_defs.ai_dl, argi)) {
671 global->usage = AOM_USAGE_ALL_INTRA; // All intra usage
672 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_nv12, argi)) {
673 global->color_type = NV12;
674 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_yv12, argi)) {
675 global->color_type = YV12;
676 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i420, argi)) {
677 global->color_type = I420;
678 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i422, argi)) {
679 global->color_type = I422;
680 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_i444, argi)) {
681 global->color_type = I444;
682 } else if (arg_match(&arg, &g_av1_codec_arg_defs.quietarg, argi)) {
683 global->quiet = 1;
684 } else if (arg_match(&arg, &g_av1_codec_arg_defs.verbosearg, argi)) {
685 global->verbose = 1;
686 } else if (arg_match(&arg, &g_av1_codec_arg_defs.limit, argi)) {
687 global->limit = arg_parse_uint(&arg);
688 } else if (arg_match(&arg, &g_av1_codec_arg_defs.skip, argi)) {
689 global->skip_frames = arg_parse_uint(&arg);
690 } else if (arg_match(&arg, &g_av1_codec_arg_defs.psnrarg, argi)) {
691 if (arg.val)
692 global->show_psnr = arg_parse_int(&arg);
693 else
694 global->show_psnr = 1;
695 } else if (arg_match(&arg, &g_av1_codec_arg_defs.recontest, argi)) {
696 global->test_decode = arg_parse_enum_or_int(&arg);
697 } else if (arg_match(&arg, &g_av1_codec_arg_defs.framerate, argi)) {
698 global->framerate = arg_parse_rational(&arg);
699 validate_positive_rational(arg.name, &global->framerate);
700 global->have_framerate = 1;
701 } else if (arg_match(&arg, &g_av1_codec_arg_defs.debugmode, argi)) {
702 global->debug = 1;
703 } else if (arg_match(&arg, &g_av1_codec_arg_defs.q_hist_n, argi)) {
704 global->show_q_hist_buckets = arg_parse_uint(&arg);
705 } else if (arg_match(&arg, &g_av1_codec_arg_defs.rate_hist_n, argi)) {
706 global->show_rate_hist_buckets = arg_parse_uint(&arg);
707 } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warnings, argi)) {
708 global->disable_warnings = 1;
709 } else if (arg_match(&arg, &g_av1_codec_arg_defs.disable_warning_prompt,
710 argi)) {
711 global->disable_warning_prompt = 1;
712 } else {
713 argj++;
714 }
715 }
716
717 if (global->pass) {
718 /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
719 if (global->pass > global->passes) {
720 aom_tools_warn("Assuming --pass=%d implies --passes=%d\n", global->pass,
721 global->pass);
722 global->passes = global->pass;
723 }
724 }
725 /* Validate global config */
726 if (global->passes == 0) {
727#if CONFIG_AV1_ENCODER
728 // Make default AV1 passes = 2 until there is a better quality 1-pass
729 // encoder
730 if (global->codec != NULL)
731 global->passes =
732 (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0 &&
733 global->usage != AOM_USAGE_REALTIME)
734 ? 2
735 : 1;
736#else
737 global->passes = 1;
738#endif
739 }
740
741 if (global->usage == AOM_USAGE_REALTIME && global->passes > 1) {
742 aom_tools_warn("Enforcing one-pass encoding in realtime mode\n");
743 if (global->pass > 1)
744 die("Error: Invalid --pass=%d for one-pass encoding\n", global->pass);
745 global->passes = 1;
746 }
747
748 if (global->usage == AOM_USAGE_ALL_INTRA && global->passes > 1) {
749 aom_tools_warn("Enforcing one-pass encoding in all intra mode\n");
750 global->passes = 1;
751 }
752}
753
754static void open_input_file(struct AvxInputContext *input,
756 /* Parse certain options from the input file, if possible */
757 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
758 : set_binary_mode(stdin);
759
760 if (!input->file) fatal("Failed to open input file");
761
762 if (!fseeko(input->file, 0, SEEK_END)) {
763 /* Input file is seekable. Figure out how long it is, so we can get
764 * progress info.
765 */
766 input->length = ftello(input->file);
767 rewind(input->file);
768 }
769
770 /* Default to 1:1 pixel aspect ratio. */
771 input->pixel_aspect_ratio.numerator = 1;
772 input->pixel_aspect_ratio.denominator = 1;
773
774 /* For RAW input sources, these bytes will applied on the first frame
775 * in read_frame().
776 */
777 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
778 input->detect.position = 0;
779
780 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
781 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
782 input->only_i420) >= 0) {
783 input->file_type = FILE_TYPE_Y4M;
784 input->width = input->y4m.pic_w;
785 input->height = input->y4m.pic_h;
786 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
787 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
788 input->framerate.numerator = input->y4m.fps_n;
789 input->framerate.denominator = input->y4m.fps_d;
790 input->fmt = input->y4m.aom_fmt;
791 input->bit_depth = input->y4m.bit_depth;
792 input->color_range = input->y4m.color_range;
793 } else
794 fatal("Unsupported Y4M stream.");
795 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
796 fatal("IVF is not supported as input.");
797 } else {
798 input->file_type = FILE_TYPE_RAW;
799 }
800}
801
802static void close_input_file(struct AvxInputContext *input) {
803 fclose(input->file);
804 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
805}
806
807static struct stream_state *new_stream(struct AvxEncoderConfig *global,
808 struct stream_state *prev) {
809 struct stream_state *stream;
810
811 stream = calloc(1, sizeof(*stream));
812 if (stream == NULL) {
813 fatal("Failed to allocate new stream.");
814 }
815
816 if (prev) {
817 *stream = *prev;
818 stream->index++;
819 prev->next = stream;
820 } else {
821 aom_codec_err_t res;
822
823 /* Populate encoder configuration */
824 res = aom_codec_enc_config_default(global->codec, &stream->config.cfg,
825 global->usage);
826 if (res) fatal("Failed to get config: %s\n", aom_codec_err_to_string(res));
827
828 /* Change the default timebase to a high enough value so that the
829 * encoder will always create strictly increasing timestamps.
830 */
831 stream->config.cfg.g_timebase.den = 1000;
832
833 /* Never use the library's default resolution, require it be parsed
834 * from the file or set on the command line.
835 */
836 stream->config.cfg.g_w = 0;
837 stream->config.cfg.g_h = 0;
838
839 /* Initialize remaining stream parameters */
840 stream->config.write_webm = 1;
841 stream->config.write_ivf = 0;
842
843#if CONFIG_WEBM_IO
844 stream->config.stereo_fmt = STEREO_FORMAT_MONO;
845 stream->webm_ctx.last_pts_ns = -1;
846 stream->webm_ctx.writer = NULL;
847 stream->webm_ctx.segment = NULL;
848#endif
849
850 /* Allows removal of the application version from the EBML tags */
851 stream->webm_ctx.debug = global->debug;
852 stream->config.cfg.encoder_cfg = global->encoder_config;
853 }
854
855 /* Output files must be specified for each stream */
856 stream->config.out_fn = NULL;
857 stream->config.two_pass_input = NULL;
858 stream->config.two_pass_output = NULL;
859 stream->config.two_pass_width = 0;
860 stream->config.two_pass_height = 0;
861
862 stream->next = NULL;
863 return stream;
864}
865
866static void set_config_arg_ctrls(struct stream_config *config, int key,
867 const struct arg *arg) {
868 int j;
869 if (key == AV1E_SET_FILM_GRAIN_TABLE) {
870 config->film_grain_filename = arg->val;
871 return;
872 }
873
874 // For target level, the settings should accumulate rather than overwrite,
875 // so we simply append it.
877 j = config->arg_ctrl_cnt;
878 assert(j < ARG_CTRL_CNT_MAX);
879 config->arg_ctrls[j][0] = key;
880 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
881 ++config->arg_ctrl_cnt;
882 return;
883 }
884
885 /* Point either to the next free element or the first instance of this
886 * control.
887 */
888 for (j = 0; j < config->arg_ctrl_cnt; j++)
889 if (config->arg_ctrls[j][0] == key) break;
890
891 /* Update/insert */
892 assert(j < ARG_CTRL_CNT_MAX);
893 config->arg_ctrls[j][0] = key;
894 config->arg_ctrls[j][1] = arg_parse_enum_or_int(arg);
895
896 if (key == AOME_SET_ENABLEAUTOALTREF && config->arg_ctrls[j][1] > 1) {
897 aom_tools_warn(
898 "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
899 config->arg_ctrls[j][1] = 1;
900 }
901
902 if (j == config->arg_ctrl_cnt) config->arg_ctrl_cnt++;
903}
904
905static void set_config_arg_key_vals(struct stream_config *config,
906 const char *name, const struct arg *arg) {
907 int j;
908 const char *val = arg->val;
909 // For target level, the settings should accumulate rather than overwrite,
910 // so we simply append it.
911 if (strcmp(name, "target-seq-level-idx") == 0) {
912 j = config->arg_key_val_cnt;
913 assert(j < ARG_KEY_VAL_CNT_MAX);
914 config->arg_key_vals[j][0] = name;
915 config->arg_key_vals[j][1] = val;
916 ++config->arg_key_val_cnt;
917 return;
918 }
919
920 /* Point either to the next free element or the first instance of this
921 * option.
922 */
923 for (j = 0; j < config->arg_key_val_cnt; j++)
924 if (strcmp(name, config->arg_key_vals[j][0]) == 0) break;
925
926 /* Update/insert */
927 assert(j < ARG_KEY_VAL_CNT_MAX);
928 config->arg_key_vals[j][0] = name;
929 config->arg_key_vals[j][1] = val;
930
931 if (strcmp(name, g_av1_codec_arg_defs.auto_altref.long_name) == 0) {
932 int auto_altref = arg_parse_int(arg);
933 if (auto_altref > 1) {
934 aom_tools_warn(
935 "auto-alt-ref > 1 is deprecated... setting auto-alt-ref=1\n");
936 config->arg_key_vals[j][1] = "1";
937 }
938 }
939
940 if (j == config->arg_key_val_cnt) config->arg_key_val_cnt++;
941}
942
943static int parse_stream_params(struct AvxEncoderConfig *global,
944 struct stream_state *stream, char **argv) {
945 char **argi, **argj;
946 struct arg arg;
947 const arg_def_t *const *ctrl_args = no_args;
948 const arg_def_t *const *key_val_args = no_args;
949 const int *ctrl_args_map = NULL;
950 struct stream_config *config = &stream->config;
951 int eos_mark_found = 0;
952 int webm_forced = 0;
953
954 // Handle codec specific options
955 if (0) {
956#if CONFIG_AV1_ENCODER
957 } else if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
958 // TODO(jingning): Reuse AV1 specific encoder configuration parameters.
959 // Consider to expand this set for AV1 encoder control.
960#if __STDC_VERSION__ >= 201112L
961 _Static_assert(NELEMENTS(av1_ctrl_args) == NELEMENTS(av1_arg_ctrl_map),
962 "The av1_ctrl_args and av1_arg_ctrl_map arrays must be of "
963 "the same size.");
964#else
965 assert(NELEMENTS(av1_ctrl_args) == NELEMENTS(av1_arg_ctrl_map));
966#endif
967 ctrl_args = av1_ctrl_args;
968 ctrl_args_map = av1_arg_ctrl_map;
969 key_val_args = av1_key_val_args;
970#endif
971 }
972
973 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
974 arg.argv_step = 1;
975
976 /* Once we've found an end-of-stream marker (--) we want to continue
977 * shifting arguments but not consuming them.
978 */
979 if (eos_mark_found) {
980 argj++;
981 continue;
982 } else if (!strcmp(*argj, "--")) {
983 eos_mark_found = 1;
984 continue;
985 }
986
987 if (arg_match(&arg, &g_av1_codec_arg_defs.outputfile, argi)) {
988 config->out_fn = arg.val;
989 if (!webm_forced) {
990 const size_t out_fn_len = strlen(config->out_fn);
991 if (out_fn_len >= 4 &&
992 !strcmp(config->out_fn + out_fn_len - 4, ".ivf")) {
993 config->write_webm = 0;
994 config->write_ivf = 1;
995 } else if (out_fn_len >= 4 &&
996 !strcmp(config->out_fn + out_fn_len - 4, ".obu")) {
997 config->write_webm = 0;
998 config->write_ivf = 0;
999 }
1000 }
1001 } else if (arg_match(&arg, &g_av1_codec_arg_defs.fpf_name, argi)) {
1002 config->stats_fn = arg.val;
1003 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_webm, argi)) {
1004#if CONFIG_WEBM_IO
1005 config->write_webm = 1;
1006 webm_forced = 1;
1007#else
1008 die("Error: --webm specified but webm is disabled.");
1009#endif
1010 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_ivf, argi)) {
1011 config->write_webm = 0;
1012 config->write_ivf = 1;
1013 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_obu, argi)) {
1014 config->write_webm = 0;
1015 config->write_ivf = 0;
1016 } else if (arg_match(&arg, &g_av1_codec_arg_defs.threads, argi)) {
1017 config->cfg.g_threads = arg_parse_uint(&arg);
1018 } else if (arg_match(&arg, &g_av1_codec_arg_defs.profile, argi)) {
1019 config->cfg.g_profile = arg_parse_uint(&arg);
1020 } else if (arg_match(&arg, &g_av1_codec_arg_defs.width, argi)) {
1021 config->cfg.g_w = arg_parse_uint(&arg);
1022 } else if (arg_match(&arg, &g_av1_codec_arg_defs.height, argi)) {
1023 config->cfg.g_h = arg_parse_uint(&arg);
1024 } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_width,
1025 argi)) {
1026 config->cfg.g_forced_max_frame_width = arg_parse_uint(&arg);
1027 } else if (arg_match(&arg, &g_av1_codec_arg_defs.forced_max_frame_height,
1028 argi)) {
1029 config->cfg.g_forced_max_frame_height = arg_parse_uint(&arg);
1030 } else if (arg_match(&arg, &g_av1_codec_arg_defs.bitdeptharg, argi)) {
1031 config->cfg.g_bit_depth = arg_parse_enum_or_int(&arg);
1032 } else if (arg_match(&arg, &g_av1_codec_arg_defs.inbitdeptharg, argi)) {
1033 config->cfg.g_input_bit_depth = arg_parse_uint(&arg);
1034 } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_x,
1035 argi)) {
1036 stream->chroma_subsampling_x = arg_parse_uint(&arg);
1037 } else if (arg_match(&arg, &g_av1_codec_arg_defs.input_chroma_subsampling_y,
1038 argi)) {
1039 stream->chroma_subsampling_y = arg_parse_uint(&arg);
1040#if CONFIG_WEBM_IO
1041 } else if (arg_match(&arg, &g_av1_codec_arg_defs.stereo_mode, argi)) {
1042 config->stereo_fmt = arg_parse_enum_or_int(&arg);
1043#endif
1044 } else if (arg_match(&arg, &g_av1_codec_arg_defs.timebase, argi)) {
1045 config->cfg.g_timebase = arg_parse_rational(&arg);
1046 validate_positive_rational(arg.name, &config->cfg.g_timebase);
1047 } else if (arg_match(&arg, &g_av1_codec_arg_defs.global_error_resilient,
1048 argi)) {
1049 config->cfg.g_error_resilient = arg_parse_uint(&arg);
1050 } else if (arg_match(&arg, &g_av1_codec_arg_defs.lag_in_frames, argi)) {
1051 config->cfg.g_lag_in_frames = arg_parse_uint(&arg);
1052 } else if (arg_match(&arg, &g_av1_codec_arg_defs.large_scale_tile, argi)) {
1053 config->cfg.large_scale_tile = arg_parse_uint(&arg);
1054 if (config->cfg.large_scale_tile) {
1055 global->codec = get_aom_encoder_by_short_name("av1");
1056 }
1057 } else if (arg_match(&arg, &g_av1_codec_arg_defs.monochrome, argi)) {
1058 config->cfg.monochrome = 1;
1059 } else if (arg_match(&arg, &g_av1_codec_arg_defs.full_still_picture_hdr,
1060 argi)) {
1061 config->cfg.full_still_picture_hdr = 1;
1062 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_16bit_internal,
1063 argi)) {
1064 config->use_16bit_internal = CONFIG_AV1_HIGHBITDEPTH;
1065 if (!config->use_16bit_internal) {
1066 aom_tools_warn("%s option ignored with CONFIG_AV1_HIGHBITDEPTH=0.\n",
1067 arg.name);
1068 }
1069 } else if (arg_match(&arg, &g_av1_codec_arg_defs.dropframe_thresh, argi)) {
1070 config->cfg.rc_dropframe_thresh = arg_parse_uint(&arg);
1071 } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_mode, argi)) {
1072 config->cfg.rc_resize_mode = arg_parse_uint(&arg);
1073 } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_denominator,
1074 argi)) {
1075 config->cfg.rc_resize_denominator = arg_parse_uint(&arg);
1076 } else if (arg_match(&arg, &g_av1_codec_arg_defs.resize_kf_denominator,
1077 argi)) {
1078 config->cfg.rc_resize_kf_denominator = arg_parse_uint(&arg);
1079 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_mode, argi)) {
1080 config->cfg.rc_superres_mode = arg_parse_uint(&arg);
1081 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_denominator,
1082 argi)) {
1083 config->cfg.rc_superres_denominator = arg_parse_uint(&arg);
1084 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_denominator,
1085 argi)) {
1086 config->cfg.rc_superres_kf_denominator = arg_parse_uint(&arg);
1087 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_qthresh, argi)) {
1088 config->cfg.rc_superres_qthresh = arg_parse_uint(&arg);
1089 } else if (arg_match(&arg, &g_av1_codec_arg_defs.superres_kf_qthresh,
1090 argi)) {
1091 config->cfg.rc_superres_kf_qthresh = arg_parse_uint(&arg);
1092 } else if (arg_match(&arg, &g_av1_codec_arg_defs.end_usage, argi)) {
1093 config->cfg.rc_end_usage = arg_parse_enum_or_int(&arg);
1094 } else if (arg_match(&arg, &g_av1_codec_arg_defs.target_bitrate, argi)) {
1095 config->cfg.rc_target_bitrate = arg_parse_uint(&arg);
1096 } else if (arg_match(&arg, &g_av1_codec_arg_defs.min_quantizer, argi)) {
1097 config->cfg.rc_min_quantizer = arg_parse_uint(&arg);
1098 } else if (arg_match(&arg, &g_av1_codec_arg_defs.max_quantizer, argi)) {
1099 config->cfg.rc_max_quantizer = arg_parse_uint(&arg);
1100 } else if (arg_match(&arg, &g_av1_codec_arg_defs.undershoot_pct, argi)) {
1101 config->cfg.rc_undershoot_pct = arg_parse_uint(&arg);
1102 } else if (arg_match(&arg, &g_av1_codec_arg_defs.overshoot_pct, argi)) {
1103 config->cfg.rc_overshoot_pct = arg_parse_uint(&arg);
1104 } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_sz, argi)) {
1105 config->cfg.rc_buf_sz = arg_parse_uint(&arg);
1106 } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_initial_sz, argi)) {
1107 config->cfg.rc_buf_initial_sz = arg_parse_uint(&arg);
1108 } else if (arg_match(&arg, &g_av1_codec_arg_defs.buf_optimal_sz, argi)) {
1109 config->cfg.rc_buf_optimal_sz = arg_parse_uint(&arg);
1110 } else if (arg_match(&arg, &g_av1_codec_arg_defs.bias_pct, argi)) {
1111 config->cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
1112 if (global->passes < 2)
1113 aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1114 } else if (arg_match(&arg, &g_av1_codec_arg_defs.minsection_pct, argi)) {
1115 config->cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
1116
1117 if (global->passes < 2)
1118 aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1119 } else if (arg_match(&arg, &g_av1_codec_arg_defs.maxsection_pct, argi)) {
1120 config->cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
1121
1122 if (global->passes < 2)
1123 aom_tools_warn("option %s ignored in one-pass mode.\n", arg.name);
1124 } else if (arg_match(&arg, &g_av1_codec_arg_defs.fwd_kf_enabled, argi)) {
1125 config->cfg.fwd_kf_enabled = arg_parse_uint(&arg);
1126 } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_min_dist, argi)) {
1127 config->cfg.kf_min_dist = arg_parse_uint(&arg);
1128 } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_max_dist, argi)) {
1129 config->cfg.kf_max_dist = arg_parse_uint(&arg);
1130 } else if (arg_match(&arg, &g_av1_codec_arg_defs.kf_disabled, argi)) {
1131 config->cfg.kf_mode = AOM_KF_DISABLED;
1132 } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_dist, argi)) {
1133 config->cfg.sframe_dist = arg_parse_uint(&arg);
1134 } else if (arg_match(&arg, &g_av1_codec_arg_defs.sframe_mode, argi)) {
1135 config->cfg.sframe_mode = arg_parse_uint(&arg);
1136 } else if (arg_match(&arg, &g_av1_codec_arg_defs.save_as_annexb, argi)) {
1137 config->cfg.save_as_annexb = arg_parse_uint(&arg);
1138 } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_width, argi)) {
1139 config->cfg.tile_width_count =
1140 arg_parse_list(&arg, config->cfg.tile_widths, MAX_TILE_WIDTHS);
1141 } else if (arg_match(&arg, &g_av1_codec_arg_defs.tile_height, argi)) {
1142 config->cfg.tile_height_count =
1143 arg_parse_list(&arg, config->cfg.tile_heights, MAX_TILE_HEIGHTS);
1144#if CONFIG_TUNE_VMAF
1145 } else if (arg_match(&arg, &g_av1_codec_arg_defs.vmaf_model_path, argi)) {
1146 config->vmaf_model_path = arg.val;
1147#endif
1148 } else if (arg_match(&arg, &g_av1_codec_arg_defs.partition_info_path,
1149 argi)) {
1150 config->partition_info_path = arg.val;
1151 } else if (arg_match(&arg, &g_av1_codec_arg_defs.enable_rate_guide_deltaq,
1152 argi)) {
1153 config->enable_rate_guide_deltaq = arg_parse_uint(&arg);
1154 } else if (arg_match(&arg, &g_av1_codec_arg_defs.rate_distribution_info,
1155 argi)) {
1156 config->rate_distribution_info = arg.val;
1157 } else if (arg_match(&arg,
1158 &g_av1_codec_arg_defs.enable_low_complexity_decode,
1159 argi)) {
1160 config->enable_low_complexity_decode = arg_parse_uint(&arg);
1161 } else if (arg_match(&arg, &g_av1_codec_arg_defs.use_fixed_qp_offsets,
1162 argi)) {
1163 config->cfg.use_fixed_qp_offsets = arg_parse_uint(&arg);
1164 } else if (arg_match(&arg, &g_av1_codec_arg_defs.fixed_qp_offsets, argi)) {
1165 config->cfg.use_fixed_qp_offsets = 1;
1166 } else if (global->usage == AOM_USAGE_REALTIME &&
1167 arg_match(&arg, &g_av1_codec_arg_defs.enable_restoration,
1168 argi)) {
1169 if (arg_parse_uint(&arg) == 1) {
1170 aom_tools_warn("non-zero %s option ignored in realtime mode.\n",
1171 arg.name);
1172 }
1173 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_input, argi)) {
1174 config->two_pass_input = arg.val;
1175 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_output, argi)) {
1176 config->two_pass_output = arg.val;
1177 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_width, argi)) {
1178 config->two_pass_width = arg_parse_int(&arg);
1179 } else if (arg_match(&arg, &g_av1_codec_arg_defs.two_pass_height, argi)) {
1180 config->two_pass_height = arg_parse_int(&arg);
1181 } else {
1182 int i, match = 0;
1183 // check if the control ID API supports this arg
1184 if (ctrl_args_map) {
1185 for (i = 0; ctrl_args[i]; i++) {
1186 if (arg_match(&arg, ctrl_args[i], argi)) {
1187 match = 1;
1188 set_config_arg_ctrls(config, ctrl_args_map[i], &arg);
1189 break;
1190 }
1191 }
1192 }
1193 if (!match) {
1194 // check if the key & value API supports this arg
1195 for (i = 0; key_val_args[i]; i++) {
1196 if (arg_match(&arg, key_val_args[i], argi)) {
1197 match = 1;
1198 set_config_arg_key_vals(config, key_val_args[i]->long_name, &arg);
1199 break;
1200 }
1201 }
1202 }
1203 if (!match) argj++;
1204 }
1205 }
1206 config->use_16bit_internal |= config->cfg.g_bit_depth > AOM_BITS_8;
1207
1208 if (global->usage == AOM_USAGE_REALTIME && config->cfg.g_lag_in_frames != 0) {
1209 aom_tools_warn("non-zero lag-in-frames option ignored in realtime mode.\n");
1210 config->cfg.g_lag_in_frames = 0;
1211 }
1212
1213 if (global->usage == AOM_USAGE_ALL_INTRA) {
1214 if (config->cfg.g_lag_in_frames != 0) {
1215 aom_tools_warn(
1216 "non-zero lag-in-frames option ignored in all intra mode.\n");
1217 config->cfg.g_lag_in_frames = 0;
1218 }
1219 if (config->cfg.kf_max_dist != 0) {
1220 aom_tools_warn(
1221 "non-zero max key frame distance option ignored in all intra "
1222 "mode.\n");
1223 config->cfg.kf_max_dist = 0;
1224 }
1225 }
1226
1227 // set the passes field using key & val API
1228 if (config->arg_key_val_cnt >= ARG_KEY_VAL_CNT_MAX) {
1229 die("Not enough buffer for the key & value API.");
1230 }
1231 config->arg_key_vals[config->arg_key_val_cnt][0] = "passes";
1232 switch (global->passes) {
1233 case 0: config->arg_key_vals[config->arg_key_val_cnt][1] = "0"; break;
1234 case 1: config->arg_key_vals[config->arg_key_val_cnt][1] = "1"; break;
1235 case 2: config->arg_key_vals[config->arg_key_val_cnt][1] = "2"; break;
1236 case 3: config->arg_key_vals[config->arg_key_val_cnt][1] = "3"; break;
1237 default: die("Invalid value of --passes.");
1238 }
1239 config->arg_key_val_cnt++;
1240
1241 // set the two_pass_output field
1242 if (!config->two_pass_output && global->passes == 3) {
1243 // If not specified, set the name of two_pass_output file here.
1244 snprintf(stream->tmp_out_fn, sizeof(stream->tmp_out_fn),
1245 "%.980s_pass2_%d.ivf", stream->config.out_fn, stream->index);
1246 stream->config.two_pass_output = stream->tmp_out_fn;
1247 }
1248 if (config->two_pass_output) {
1249 config->arg_key_vals[config->arg_key_val_cnt][0] = "two-pass-output";
1250 config->arg_key_vals[config->arg_key_val_cnt][1] = config->two_pass_output;
1251 config->arg_key_val_cnt++;
1252 }
1253
1254 return eos_mark_found;
1255}
1256
1257#define FOREACH_STREAM(iterator, list) \
1258 for (struct stream_state *iterator = list; iterator; \
1259 iterator = iterator->next)
1260
1261static void validate_stream_config(const struct stream_state *stream,
1262 const struct AvxEncoderConfig *global) {
1263 const struct stream_state *streami;
1264 (void)global;
1265
1266 if (!stream->config.cfg.g_w || !stream->config.cfg.g_h)
1267 fatal(
1268 "Stream %d: Specify stream dimensions with --width (-w) "
1269 " and --height (-h)",
1270 stream->index);
1271
1272 /* Even if bit depth is set on the command line flag to be lower,
1273 * it is upgraded to at least match the input bit depth.
1274 */
1275 assert(stream->config.cfg.g_input_bit_depth <=
1276 (unsigned int)stream->config.cfg.g_bit_depth);
1277
1278 for (streami = stream; streami; streami = streami->next) {
1279 /* All streams require output files */
1280 if (!streami->config.out_fn)
1281 fatal("Stream %d: Output file is required (specify with -o)",
1282 streami->index);
1283
1284 /* Check for two streams outputting to the same file */
1285 if (streami != stream) {
1286 const char *a = stream->config.out_fn;
1287 const char *b = streami->config.out_fn;
1288 if (!strcmp(a, b) && strcmp(a, "/dev/null") && strcmp(a, ":nul"))
1289 fatal("Stream %d: duplicate output file (from stream %d)",
1290 streami->index, stream->index);
1291 }
1292
1293 /* Check for two streams sharing a stats file. */
1294 if (streami != stream) {
1295 const char *a = stream->config.stats_fn;
1296 const char *b = streami->config.stats_fn;
1297 if (a && b && !strcmp(a, b))
1298 fatal("Stream %d: duplicate stats file (from stream %d)",
1299 streami->index, stream->index);
1300 }
1301 }
1302}
1303
1304static void set_stream_dimensions(struct stream_state *stream, unsigned int w,
1305 unsigned int h) {
1306 if (!stream->config.cfg.g_w) {
1307 if (!stream->config.cfg.g_h)
1308 stream->config.cfg.g_w = w;
1309 else
1310 stream->config.cfg.g_w = w * stream->config.cfg.g_h / h;
1311 }
1312 if (!stream->config.cfg.g_h) {
1313 stream->config.cfg.g_h = h * stream->config.cfg.g_w / w;
1314 }
1315}
1316
1317static const char *file_type_to_string(enum VideoFileType t) {
1318 switch (t) {
1319 case FILE_TYPE_RAW: return "RAW";
1320 case FILE_TYPE_Y4M: return "Y4M";
1321 default: return "Other";
1322 }
1323}
1324
1325static void show_stream_config(struct stream_state *stream,
1326 struct AvxEncoderConfig *global,
1327 struct AvxInputContext *input) {
1328#define SHOW(field) \
1329 fprintf(stderr, " %-28s = %d\n", #field, stream->config.cfg.field)
1330
1331 if (stream->index == 0) {
1332 fprintf(stderr, "Codec: %s\n", aom_codec_iface_name(global->codec));
1333 fprintf(stderr, "Source file: %s File Type: %s Format: %s\n",
1334 input->filename, file_type_to_string(input->file_type),
1335 image_format_to_string(input->fmt));
1336 }
1337 if (stream->next || stream->index)
1338 fprintf(stderr, "\nStream Index: %d\n", stream->index);
1339 fprintf(stderr, "Destination file: %s\n", stream->config.out_fn);
1340 fprintf(stderr, "Coding path: %s\n",
1341 stream->config.use_16bit_internal ? "HBD" : "LBD");
1342 fprintf(stderr, "Encoder parameters:\n");
1343
1344 SHOW(g_usage);
1345 SHOW(g_threads);
1346 SHOW(g_profile);
1347 SHOW(g_w);
1348 SHOW(g_h);
1349 SHOW(g_bit_depth);
1350 SHOW(g_input_bit_depth);
1351 SHOW(g_timebase.num);
1352 SHOW(g_timebase.den);
1353 SHOW(g_error_resilient);
1354 SHOW(g_pass);
1355 SHOW(g_lag_in_frames);
1356 SHOW(large_scale_tile);
1357 SHOW(rc_dropframe_thresh);
1358 SHOW(rc_resize_mode);
1359 SHOW(rc_resize_denominator);
1360 SHOW(rc_resize_kf_denominator);
1361 SHOW(rc_superres_mode);
1362 SHOW(rc_superres_denominator);
1363 SHOW(rc_superres_kf_denominator);
1364 SHOW(rc_superres_qthresh);
1365 SHOW(rc_superres_kf_qthresh);
1366 SHOW(rc_end_usage);
1367 SHOW(rc_target_bitrate);
1368 SHOW(rc_min_quantizer);
1369 SHOW(rc_max_quantizer);
1370 SHOW(rc_undershoot_pct);
1371 SHOW(rc_overshoot_pct);
1372 SHOW(rc_buf_sz);
1373 SHOW(rc_buf_initial_sz);
1374 SHOW(rc_buf_optimal_sz);
1375 SHOW(rc_2pass_vbr_bias_pct);
1376 SHOW(rc_2pass_vbr_minsection_pct);
1377 SHOW(rc_2pass_vbr_maxsection_pct);
1378 SHOW(fwd_kf_enabled);
1379 SHOW(kf_mode);
1380 SHOW(kf_min_dist);
1381 SHOW(kf_max_dist);
1382
1383#define SHOW_PARAMS(field) \
1384 fprintf(stderr, " %-28s = %d\n", #field, \
1385 stream->config.cfg.encoder_cfg.field)
1386 if (global->encoder_config.init_by_cfg_file) {
1387 SHOW_PARAMS(super_block_size);
1388 SHOW_PARAMS(max_partition_size);
1389 SHOW_PARAMS(min_partition_size);
1390 SHOW_PARAMS(disable_ab_partition_type);
1391 SHOW_PARAMS(disable_rect_partition_type);
1392 SHOW_PARAMS(disable_1to4_partition_type);
1393 SHOW_PARAMS(disable_flip_idtx);
1394 SHOW_PARAMS(disable_cdef);
1395 SHOW_PARAMS(disable_lr);
1396 SHOW_PARAMS(disable_obmc);
1397 SHOW_PARAMS(disable_warp_motion);
1398 SHOW_PARAMS(disable_global_motion);
1399 SHOW_PARAMS(disable_dist_wtd_comp);
1400 SHOW_PARAMS(disable_diff_wtd_comp);
1401 SHOW_PARAMS(disable_inter_intra_comp);
1402 SHOW_PARAMS(disable_masked_comp);
1403 SHOW_PARAMS(disable_one_sided_comp);
1404 SHOW_PARAMS(disable_palette);
1405 SHOW_PARAMS(disable_intrabc);
1406 SHOW_PARAMS(disable_cfl);
1407 SHOW_PARAMS(disable_smooth_intra);
1408 SHOW_PARAMS(disable_filter_intra);
1409 SHOW_PARAMS(disable_dual_filter);
1410 SHOW_PARAMS(disable_intra_angle_delta);
1411 SHOW_PARAMS(disable_intra_edge_filter);
1412 SHOW_PARAMS(disable_tx_64x64);
1413 SHOW_PARAMS(disable_smooth_inter_intra);
1414 SHOW_PARAMS(disable_inter_inter_wedge);
1415 SHOW_PARAMS(disable_inter_intra_wedge);
1416 SHOW_PARAMS(disable_paeth_intra);
1417 SHOW_PARAMS(disable_trellis_quant);
1418 SHOW_PARAMS(disable_ref_frame_mv);
1419 SHOW_PARAMS(reduced_reference_set);
1420 SHOW_PARAMS(reduced_tx_type_set);
1421 }
1422}
1423
1424static void open_output_file(struct stream_state *stream,
1425 struct AvxEncoderConfig *global,
1426 const struct AvxRational *pixel_aspect_ratio,
1427 const char *encoder_settings) {
1428 const char *fn = stream->config.out_fn;
1429 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1430
1431 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1432
1433 stream->file = strcmp(fn, "-") ? fopen(fn, "wb") : set_binary_mode(stdout);
1434
1435 if (!stream->file) fatal("Failed to open output file");
1436
1437 if (stream->config.write_webm && fseek(stream->file, 0, SEEK_CUR))
1438 fatal("WebM output to pipes not supported.");
1439
1440#if CONFIG_WEBM_IO
1441 if (stream->config.write_webm) {
1442 stream->webm_ctx.stream = stream->file;
1443 if (write_webm_file_header(&stream->webm_ctx, &stream->encoder, cfg,
1444 stream->config.stereo_fmt,
1445 get_fourcc_by_aom_encoder(global->codec),
1446 pixel_aspect_ratio, encoder_settings) != 0) {
1447 fatal("WebM writer initialization failed.");
1448 }
1449 }
1450#else
1451 (void)pixel_aspect_ratio;
1452 (void)encoder_settings;
1453#endif
1454
1455 if (!stream->config.write_webm && stream->config.write_ivf) {
1456 ivf_write_file_header(stream->file, cfg,
1457 get_fourcc_by_aom_encoder(global->codec), 0);
1458 }
1459}
1460
1461static void close_output_file(struct stream_state *stream,
1462 unsigned int fourcc) {
1463 const struct aom_codec_enc_cfg *const cfg = &stream->config.cfg;
1464
1465 if (cfg->g_pass == AOM_RC_FIRST_PASS) return;
1466
1467#if CONFIG_WEBM_IO
1468 if (stream->config.write_webm) {
1469 if (write_webm_file_footer(&stream->webm_ctx) != 0) {
1470 fatal("WebM writer finalization failed.");
1471 }
1472 }
1473#endif
1474
1475 if (!stream->config.write_webm && stream->config.write_ivf) {
1476 if (!fseek(stream->file, 0, SEEK_SET))
1477 ivf_write_file_header(stream->file, &stream->config.cfg, fourcc,
1478 stream->frames_out);
1479 }
1480
1481 fclose(stream->file);
1482}
1483
1484static void setup_pass(struct stream_state *stream,
1485 struct AvxEncoderConfig *global, int pass) {
1486 if (stream->config.stats_fn) {
1487 if (!stats_open_file(&stream->stats, stream->config.stats_fn, pass))
1488 fatal("Failed to open statistics store");
1489 } else {
1490 if (!stats_open_mem(&stream->stats, pass))
1491 fatal("Failed to open statistics store");
1492 }
1493
1494 if (global->passes == 1) {
1495 stream->config.cfg.g_pass = AOM_RC_ONE_PASS;
1496 } else {
1497 switch (pass) {
1498 case 0: stream->config.cfg.g_pass = AOM_RC_FIRST_PASS; break;
1499 case 1: stream->config.cfg.g_pass = AOM_RC_SECOND_PASS; break;
1500 case 2: stream->config.cfg.g_pass = AOM_RC_THIRD_PASS; break;
1501 default: fatal("Failed to set pass");
1502 }
1503 }
1504
1505 if (pass) {
1506 stream->config.cfg.rc_twopass_stats_in = stats_get(&stream->stats);
1507 }
1508
1509 stream->cx_time = 0;
1510 stream->nbytes = 0;
1511 stream->frames_out = 0;
1512}
1513
1514static void initialize_encoder(struct stream_state *stream,
1515 struct AvxEncoderConfig *global) {
1516 int i;
1517 int flags = 0;
1518
1519 flags |= (global->show_psnr >= 1) ? AOM_CODEC_USE_PSNR : 0;
1520 flags |= stream->config.use_16bit_internal ? AOM_CODEC_USE_HIGHBITDEPTH : 0;
1521
1522 /* Construct Encoder Context */
1523 aom_codec_enc_init(&stream->encoder, global->codec, &stream->config.cfg,
1524 flags);
1525 ctx_exit_on_error(&stream->encoder, "Failed to initialize encoder");
1526
1527 for (i = 0; i < stream->config.arg_ctrl_cnt; i++) {
1528 int ctrl = stream->config.arg_ctrls[i][0];
1529 int value = stream->config.arg_ctrls[i][1];
1530 if (aom_codec_control(&stream->encoder, ctrl, value))
1531 fprintf(stderr, "Error: Tried to set control %d = %d\n", ctrl, value);
1532
1533 ctx_exit_on_error(&stream->encoder, "Failed to control codec");
1534 }
1535
1536 for (i = 0; i < stream->config.arg_key_val_cnt; i++) {
1537 const char *name = stream->config.arg_key_vals[i][0];
1538 const char *val = stream->config.arg_key_vals[i][1];
1539 if (aom_codec_set_option(&stream->encoder, name, val))
1540 fprintf(stderr, "Error: Tried to set option %s = %s\n", name, val);
1541
1542 ctx_exit_on_error(&stream->encoder, "Failed to set codec option");
1543 }
1544
1545#if CONFIG_TUNE_VMAF
1546 if (stream->config.vmaf_model_path) {
1548 stream->config.vmaf_model_path);
1549 ctx_exit_on_error(&stream->encoder, "Failed to set vmaf model path");
1550 }
1551#endif
1552 if (stream->config.partition_info_path) {
1553 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1555 stream->config.partition_info_path);
1556 ctx_exit_on_error(&stream->encoder, "Failed to set partition info path");
1557 }
1558 if (stream->config.enable_rate_guide_deltaq) {
1559 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1561 stream->config.enable_rate_guide_deltaq);
1562 ctx_exit_on_error(&stream->encoder, "Failed to enable rate guide deltaq");
1563 }
1564 if (stream->config.rate_distribution_info) {
1565 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1567 stream->config.rate_distribution_info);
1568 ctx_exit_on_error(&stream->encoder, "Failed to set rate distribution info");
1569 }
1570
1571 if (stream->config.enable_low_complexity_decode) {
1572 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
1574 stream->config.enable_low_complexity_decode);
1575 ctx_exit_on_error(&stream->encoder,
1576 "Failed to enable low complexity decode");
1577 }
1578
1579 if (stream->config.film_grain_filename) {
1581 stream->config.film_grain_filename);
1582 ctx_exit_on_error(&stream->encoder, "Failed to set film grain table");
1583 }
1585 stream->config.color_range);
1586 ctx_exit_on_error(&stream->encoder, "Failed to set color range");
1587
1588#if CONFIG_AV1_DECODER
1589 if (global->test_decode != TEST_DECODE_OFF) {
1590 aom_codec_iface_t *decoder = get_aom_decoder_by_short_name(
1591 get_short_name_by_aom_encoder(global->codec));
1592 aom_codec_dec_cfg_t cfg = { 0, 0, 0, !stream->config.use_16bit_internal };
1593 aom_codec_dec_init(&stream->decoder, decoder, &cfg, 0);
1594
1595 if (strcmp(get_short_name_by_aom_encoder(global->codec), "av1") == 0) {
1597 stream->config.cfg.large_scale_tile);
1598 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_mode");
1599
1601 stream->config.cfg.save_as_annexb);
1602 ctx_exit_on_error(&stream->decoder, "Failed to set is_annexb");
1603
1605 -1);
1606 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_row");
1607
1608 AOM_CODEC_CONTROL_TYPECHECKED(&stream->decoder, AV1_SET_DECODE_TILE_COL,
1609 -1);
1610 ctx_exit_on_error(&stream->decoder, "Failed to set decode_tile_col");
1611 }
1612 }
1613#endif
1614}
1615
1616// Convert the input image 'img' to a monochrome image. The Y plane of the
1617// output image is a shallow copy of the Y plane of the input image, therefore
1618// the input image must remain valid for the lifetime of the output image. The U
1619// and V planes of the output image are set to null pointers. The output image
1620// format is AOM_IMG_FMT_I420 because libaom does not have AOM_IMG_FMT_I400.
1621static void convert_image_to_monochrome(const struct aom_image *img,
1622 struct aom_image *monochrome_img) {
1623 *monochrome_img = *img;
1624 monochrome_img->fmt = AOM_IMG_FMT_I420;
1625 if (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1626 monochrome_img->fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
1627 }
1628 monochrome_img->monochrome = 1;
1629 monochrome_img->csp = AOM_CSP_UNKNOWN;
1630 monochrome_img->x_chroma_shift = 1;
1631 monochrome_img->y_chroma_shift = 1;
1632 monochrome_img->planes[AOM_PLANE_U] = NULL;
1633 monochrome_img->planes[AOM_PLANE_V] = NULL;
1634 monochrome_img->stride[AOM_PLANE_U] = 0;
1635 monochrome_img->stride[AOM_PLANE_V] = 0;
1636 monochrome_img->sz = 0;
1637 monochrome_img->bps = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
1638 monochrome_img->img_data = NULL;
1639 monochrome_img->img_data_owner = 0;
1640 monochrome_img->self_allocd = 0;
1641}
1642
1643static void encode_frame(struct stream_state *stream,
1644 struct AvxEncoderConfig *global, struct aom_image *img,
1645 unsigned int frames_in) {
1646 aom_codec_pts_t frame_start, next_frame_start;
1647 struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1648 struct aom_usec_timer timer;
1649
1650 frame_start =
1651 (cfg->g_timebase.den * (int64_t)(frames_in - 1) * global->framerate.den) /
1652 cfg->g_timebase.num / global->framerate.num;
1653 next_frame_start =
1654 (cfg->g_timebase.den * (int64_t)(frames_in)*global->framerate.den) /
1655 cfg->g_timebase.num / global->framerate.num;
1656
1657 /* Scale if necessary */
1658 if (img) {
1659 if ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) &&
1660 (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1661 if (img->fmt != AOM_IMG_FMT_I42016) {
1662 fprintf(stderr, "%s can only scale 4:2:0 inputs\n", exec_name);
1663 exit(EXIT_FAILURE);
1664 }
1665#if CONFIG_LIBYUV
1666 if (!stream->img) {
1667 stream->img =
1668 aom_img_alloc(NULL, AOM_IMG_FMT_I42016, cfg->g_w, cfg->g_h, 16);
1669 }
1670 I420Scale_16(
1671 (uint16_t *)img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y] / 2,
1672 (uint16_t *)img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U] / 2,
1673 (uint16_t *)img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V] / 2,
1674 img->d_w, img->d_h, (uint16_t *)stream->img->planes[AOM_PLANE_Y],
1675 stream->img->stride[AOM_PLANE_Y] / 2,
1676 (uint16_t *)stream->img->planes[AOM_PLANE_U],
1677 stream->img->stride[AOM_PLANE_U] / 2,
1678 (uint16_t *)stream->img->planes[AOM_PLANE_V],
1679 stream->img->stride[AOM_PLANE_V] / 2, stream->img->d_w,
1680 stream->img->d_h, kFilterBox);
1681 img = stream->img;
1682#else
1683 stream->encoder.err = 1;
1684 ctx_exit_on_error(&stream->encoder,
1685 "Stream %d: Failed to encode frame.\n"
1686 "libyuv is required for scaling but is currently "
1687 "disabled.\n"
1688 "Be sure to specify -DCONFIG_LIBYUV=1 when running "
1689 "cmake.\n",
1690 stream->index);
1691#endif
1692 }
1693 }
1694 if (img && (img->d_w != cfg->g_w || img->d_h != cfg->g_h)) {
1695 if (img->fmt != AOM_IMG_FMT_I420 && img->fmt != AOM_IMG_FMT_YV12) {
1696 fprintf(stderr, "%s can only scale 4:2:0 8bpp inputs\n", exec_name);
1697 exit(EXIT_FAILURE);
1698 }
1699#if CONFIG_LIBYUV
1700 if (!stream->img)
1701 stream->img =
1702 aom_img_alloc(NULL, AOM_IMG_FMT_I420, cfg->g_w, cfg->g_h, 16);
1703 I420Scale(
1704 img->planes[AOM_PLANE_Y], img->stride[AOM_PLANE_Y],
1705 img->planes[AOM_PLANE_U], img->stride[AOM_PLANE_U],
1706 img->planes[AOM_PLANE_V], img->stride[AOM_PLANE_V], img->d_w, img->d_h,
1707 stream->img->planes[AOM_PLANE_Y], stream->img->stride[AOM_PLANE_Y],
1708 stream->img->planes[AOM_PLANE_U], stream->img->stride[AOM_PLANE_U],
1709 stream->img->planes[AOM_PLANE_V], stream->img->stride[AOM_PLANE_V],
1710 stream->img->d_w, stream->img->d_h, kFilterBox);
1711 img = stream->img;
1712#else
1713 stream->encoder.err = 1;
1714 ctx_exit_on_error(&stream->encoder,
1715 "Stream %d: Failed to encode frame.\n"
1716 "Scaling disabled in this configuration. \n"
1717 "To enable, configure with --enable-libyuv\n",
1718 stream->index);
1719#endif
1720 }
1721
1722 struct aom_image monochrome_img;
1723 if (img && cfg->monochrome) {
1724 convert_image_to_monochrome(img, &monochrome_img);
1725 img = &monochrome_img;
1726 }
1727
1728 aom_usec_timer_start(&timer);
1729 aom_codec_encode(&stream->encoder, img, frame_start,
1730 (uint32_t)(next_frame_start - frame_start), 0);
1731 aom_usec_timer_mark(&timer);
1732 stream->cx_time += aom_usec_timer_elapsed(&timer);
1733 ctx_exit_on_error(&stream->encoder, "Stream %d: Failed to encode frame",
1734 stream->index);
1735}
1736
1737static void update_quantizer_histogram(struct stream_state *stream) {
1738 if (stream->config.cfg.g_pass != AOM_RC_FIRST_PASS) {
1739 int q;
1740
1742 &q);
1743 ctx_exit_on_error(&stream->encoder, "Failed to read quantizer");
1744 stream->counts[q]++;
1745 }
1746}
1747
1748static void get_cx_data(struct stream_state *stream,
1749 struct AvxEncoderConfig *global, int *got_data) {
1750 const aom_codec_cx_pkt_t *pkt;
1751 const struct aom_codec_enc_cfg *cfg = &stream->config.cfg;
1752 aom_codec_iter_t iter = NULL;
1753
1754 *got_data = 0;
1755 while ((pkt = aom_codec_get_cx_data(&stream->encoder, &iter))) {
1756 static size_t fsize = 0;
1757 static FileOffset ivf_header_pos = 0;
1758
1759 switch (pkt->kind) {
1761 ++stream->frames_out;
1762 if (!global->quiet)
1763 fprintf(stderr, " %6luF", (unsigned long)pkt->data.frame.sz);
1764
1765 update_rate_histogram(stream->rate_hist, cfg, pkt);
1766#if CONFIG_WEBM_IO
1767 if (stream->config.write_webm) {
1768 if (write_webm_block(&stream->webm_ctx, cfg, pkt) != 0) {
1769 fatal("WebM writer failed.");
1770 }
1771 }
1772#endif
1773 if (!stream->config.write_webm) {
1774 if (stream->config.write_ivf) {
1775 if (pkt->data.frame.partition_id <= 0) {
1776 ivf_header_pos = ftello(stream->file);
1777 fsize = pkt->data.frame.sz;
1778
1779 ivf_write_frame_header(stream->file, pkt->data.frame.pts, fsize);
1780 } else {
1781 fsize += pkt->data.frame.sz;
1782
1783 const FileOffset currpos = ftello(stream->file);
1784 fseeko(stream->file, ivf_header_pos, SEEK_SET);
1785 ivf_write_frame_size(stream->file, fsize);
1786 fseeko(stream->file, currpos, SEEK_SET);
1787 }
1788 }
1789
1790 (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
1791 stream->file);
1792 }
1793 stream->nbytes += pkt->data.raw.sz;
1794
1795 *got_data = 1;
1796#if CONFIG_AV1_DECODER
1797 if (global->test_decode != TEST_DECODE_OFF && !stream->mismatch_seen) {
1798 aom_codec_decode(&stream->decoder, pkt->data.frame.buf,
1799 pkt->data.frame.sz, NULL);
1800 if (stream->decoder.err) {
1801 warn_or_exit_on_error(&stream->decoder,
1802 global->test_decode == TEST_DECODE_FATAL,
1803 "Failed to decode frame %d in stream %d",
1804 stream->frames_out + 1, stream->index);
1805 stream->mismatch_seen = stream->frames_out + 1;
1806 }
1807 }
1808#endif
1809 break;
1811 stream->frames_out++;
1812 stats_write(&stream->stats, pkt->data.twopass_stats.buf,
1813 pkt->data.twopass_stats.sz);
1814 stream->nbytes += pkt->data.raw.sz;
1815 break;
1816 case AOM_CODEC_PSNR_PKT:
1817
1818 if (global->show_psnr >= 1) {
1819 int i;
1820
1821 stream->psnr_sse_total[0] += pkt->data.psnr.sse[0];
1822 stream->psnr_samples_total[0] += pkt->data.psnr.samples[0];
1823 for (i = 0; i < 4; i++) {
1824 if (!global->quiet)
1825 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr[i]);
1826 stream->psnr_totals[0][i] += pkt->data.psnr.psnr[i];
1827 }
1828 stream->psnr_count[0]++;
1829
1830#if CONFIG_AV1_HIGHBITDEPTH
1831 if (stream->config.cfg.g_input_bit_depth <
1832 (unsigned int)stream->config.cfg.g_bit_depth) {
1833 stream->psnr_sse_total[1] += pkt->data.psnr.sse_hbd[0];
1834 stream->psnr_samples_total[1] += pkt->data.psnr.samples_hbd[0];
1835 for (i = 0; i < 4; i++) {
1836 if (!global->quiet)
1837 fprintf(stderr, "%.3f ", pkt->data.psnr.psnr_hbd[i]);
1838 stream->psnr_totals[1][i] += pkt->data.psnr.psnr_hbd[i];
1839 }
1840 stream->psnr_count[1]++;
1841 }
1842#endif
1843 }
1844
1845 break;
1846 default: break;
1847 }
1848 }
1849}
1850
1851static void show_psnr(struct stream_state *stream, double peak, int64_t bps) {
1852 int i;
1853 double ovpsnr;
1854
1855 if (!stream->psnr_count[0]) return;
1856
1857 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1858 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[0], peak,
1859 (double)stream->psnr_sse_total[0]);
1860 fprintf(stderr, " %.3f", ovpsnr);
1861
1862 for (i = 0; i < 4; i++) {
1863 fprintf(stderr, " %.3f", stream->psnr_totals[0][i] / stream->psnr_count[0]);
1864 }
1865 if (bps > 0) {
1866 fprintf(stderr, " %7" PRId64 " bps", bps);
1867 }
1868 fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
1869 fprintf(stderr, "\n");
1870}
1871
1872#if CONFIG_AV1_HIGHBITDEPTH
1873static void show_psnr_hbd(struct stream_state *stream, double peak,
1874 int64_t bps) {
1875 int i;
1876 double ovpsnr;
1877 // Compute PSNR based on stream bit depth
1878 if (!stream->psnr_count[1]) return;
1879
1880 fprintf(stderr, "Stream %d PSNR (Overall/Avg/Y/U/V)", stream->index);
1881 ovpsnr = sse_to_psnr((double)stream->psnr_samples_total[1], peak,
1882 (double)stream->psnr_sse_total[1]);
1883 fprintf(stderr, " %.3f", ovpsnr);
1884
1885 for (i = 0; i < 4; i++) {
1886 fprintf(stderr, " %.3f", stream->psnr_totals[1][i] / stream->psnr_count[1]);
1887 }
1888 if (bps > 0) {
1889 fprintf(stderr, " %7" PRId64 " bps", bps);
1890 }
1891 fprintf(stderr, " %7" PRId64 " ms", stream->cx_time / 1000);
1892 fprintf(stderr, "\n");
1893}
1894#endif
1895
1896static float usec_to_fps(uint64_t usec, unsigned int frames) {
1897 return (float)(usec > 0 ? frames * 1000000.0 / (float)usec : 0);
1898}
1899
1900static void test_decode(struct stream_state *stream,
1901 enum TestDecodeFatality fatal) {
1902 aom_image_t enc_img, dec_img;
1903
1904 if (stream->mismatch_seen) return;
1905
1906 /* Get the internal reference frame */
1908 &enc_img);
1910 &dec_img);
1911
1912 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1913 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1914 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1915 aom_image_t enc_hbd_img;
1916 aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1917 enc_img.d_w, enc_img.d_h, 16);
1918 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1919 enc_img = enc_hbd_img;
1920 }
1921 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1922 aom_image_t dec_hbd_img;
1923 aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH,
1924 dec_img.d_w, dec_img.d_h, 16);
1925 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1926 dec_img = dec_hbd_img;
1927 }
1928 }
1929
1930 ctx_exit_on_error(&stream->encoder, "Failed to get encoder reference frame");
1931 ctx_exit_on_error(&stream->decoder, "Failed to get decoder reference frame");
1932
1933 if (!aom_compare_img(&enc_img, &dec_img)) {
1934 int y[4], u[4], v[4];
1935 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1936 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1937 } else {
1938 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1939 }
1940 stream->decoder.err = 1;
1941 warn_or_exit_on_error(&stream->decoder, fatal == TEST_DECODE_FATAL,
1942 "Stream %d: Encode/decode mismatch on frame %d at"
1943 " Y[%d, %d] {%d/%d},"
1944 " U[%d, %d] {%d/%d},"
1945 " V[%d, %d] {%d/%d}",
1946 stream->index, stream->frames_out, y[0], y[1], y[2],
1947 y[3], u[0], u[1], u[2], u[3], v[0], v[1], v[2], v[3]);
1948 stream->mismatch_seen = stream->frames_out;
1949 }
1950
1951 aom_img_free(&enc_img);
1952 aom_img_free(&dec_img);
1953}
1954
1955static void print_time(const char *label, int64_t etl) {
1956 int64_t hours;
1957 int64_t mins;
1958 int64_t secs;
1959
1960 if (etl >= 0) {
1961 hours = etl / 3600;
1962 etl -= hours * 3600;
1963 mins = etl / 60;
1964 etl -= mins * 60;
1965 secs = etl;
1966
1967 fprintf(stderr, "[%3s %2" PRId64 ":%02" PRId64 ":%02" PRId64 "] ", label,
1968 hours, mins, secs);
1969 } else {
1970 fprintf(stderr, "[%3s unknown] ", label);
1971 }
1972}
1973
1974static void clear_stream_count_state(struct stream_state *stream) {
1975 // PSNR counters
1976 for (int k = 0; k < 2; k++) {
1977 stream->psnr_sse_total[k] = 0;
1978 stream->psnr_samples_total[k] = 0;
1979 for (int i = 0; i < 4; i++) {
1980 stream->psnr_totals[k][i] = 0;
1981 }
1982 stream->psnr_count[k] = 0;
1983 }
1984 // q hist
1985 memset(stream->counts, 0, sizeof(stream->counts));
1986}
1987
1988// aomenc will downscale the second pass if:
1989// 1. the specific pass is not given by commandline (aomenc will perform all
1990// passes)
1991// 2. there are more than 2 passes in total
1992// 3. current pass is the second pass (the parameter pass starts with 0 so
1993// pass == 1)
1994static int pass_need_downscale(int global_pass, int global_passes, int pass) {
1995 return !global_pass && global_passes > 2 && pass == 1;
1996}
1997
1998int main(int argc, const char **argv_) {
1999 int pass;
2000 aom_image_t raw;
2001 aom_image_t raw_shift;
2002 int allocated_raw_shift = 0;
2003 int do_16bit_internal = 0;
2004 int input_shift = 0;
2005 int frame_avail, got_data;
2006
2007 struct AvxInputContext input;
2008 struct AvxEncoderConfig global;
2009 struct stream_state *streams = NULL;
2010 char **argv, **argi;
2011 uint64_t cx_time = 0;
2012 int stream_cnt = 0;
2013 int res = 0;
2014 int profile_updated = 0;
2015
2016 memset(&input, 0, sizeof(input));
2017 memset(&raw, 0, sizeof(raw));
2018 exec_name = argv_[0];
2019
2020 /* Setup default input stream settings */
2021 input.framerate.numerator = 30;
2022 input.framerate.denominator = 1;
2023 input.only_i420 = 1;
2024 input.bit_depth = 0;
2025
2026 /* First parse the global configuration values, because we want to apply
2027 * other parameters on top of the default configuration provided by the
2028 * codec.
2029 */
2030 argv = argv_dup(argc - 1, argv_ + 1);
2031 if (!argv) {
2032 fprintf(stderr, "Error allocating argument list\n");
2033 return EXIT_FAILURE;
2034 }
2035 parse_global_config(&global, &argv);
2036
2037 if (argc < 2) usage_exit();
2038
2039 switch (global.color_type) {
2040 case I420: input.fmt = AOM_IMG_FMT_I420; break;
2041 case I422: input.fmt = AOM_IMG_FMT_I422; break;
2042 case I444: input.fmt = AOM_IMG_FMT_I444; break;
2043 case YV12: input.fmt = AOM_IMG_FMT_YV12; break;
2044 case NV12: input.fmt = AOM_IMG_FMT_NV12; break;
2045 }
2046
2047 {
2048 /* Now parse each stream's parameters. Using a local scope here
2049 * due to the use of 'stream' as loop variable in FOREACH_STREAM
2050 * loops
2051 */
2052 struct stream_state *stream = NULL;
2053
2054 do {
2055 stream = new_stream(&global, stream);
2056 stream_cnt++;
2057 if (!streams) streams = stream;
2058 } while (parse_stream_params(&global, stream, argv));
2059 }
2060
2061 /* Check for unrecognized options */
2062 for (argi = argv; *argi; argi++)
2063 if (argi[0][0] == '-' && argi[0][1])
2064 die("Error: Unrecognized option %s\n", *argi);
2065
2066 FOREACH_STREAM(stream, streams) {
2067 check_encoder_config(global.disable_warning_prompt, &global,
2068 &stream->config.cfg);
2069
2070 // If large_scale_tile = 1, only support to output to ivf format.
2071 if (stream->config.cfg.large_scale_tile && !stream->config.write_ivf)
2072 die("only support ivf output format while large-scale-tile=1\n");
2073 }
2074
2075 /* Handle non-option arguments */
2076 input.filename = argv[0];
2077 const char *orig_input_filename = input.filename;
2078 FOREACH_STREAM(stream, streams) {
2079 stream->orig_out_fn = stream->config.out_fn;
2080 stream->orig_width = stream->config.cfg.g_w;
2081 stream->orig_height = stream->config.cfg.g_h;
2082 stream->orig_write_ivf = stream->config.write_ivf;
2083 stream->orig_write_webm = stream->config.write_webm;
2084 }
2085
2086 if (!input.filename) {
2087 fprintf(stderr, "No input file specified!\n");
2088 usage_exit();
2089 }
2090
2091 /* Decide if other chroma subsamplings than 4:2:0 are supported */
2092 if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC)
2093 input.only_i420 = 0;
2094
2095 for (pass = global.pass ? global.pass - 1 : 0; pass < global.passes; pass++) {
2096 if (pass > 1) {
2097 FOREACH_STREAM(stream, streams) { clear_stream_count_state(stream); }
2098 }
2099
2100 int frames_in = 0, seen_frames = 0;
2101 int64_t estimated_time_left = -1;
2102 int64_t average_rate = -1;
2103 int64_t lagged_count = 0;
2104 const int need_downscale =
2105 pass_need_downscale(global.pass, global.passes, pass);
2106
2107 // Set the output to the specified two-pass output file, and
2108 // restore the width and height to the original values.
2109 FOREACH_STREAM(stream, streams) {
2110 if (need_downscale) {
2111 stream->config.out_fn = stream->config.two_pass_output;
2112 // Libaom currently only supports the ivf format for the third pass.
2113 stream->config.write_ivf = 1;
2114 stream->config.write_webm = 0;
2115 } else {
2116 stream->config.out_fn = stream->orig_out_fn;
2117 stream->config.write_ivf = stream->orig_write_ivf;
2118 stream->config.write_webm = stream->orig_write_webm;
2119 }
2120 stream->config.cfg.g_w = stream->orig_width;
2121 stream->config.cfg.g_h = stream->orig_height;
2122 }
2123
2124 // For second pass in three-pass encoding, set the input to
2125 // the given two-pass-input file if available. If the scaled input is not
2126 // given, we will attempt to re-scale the original input.
2127 input.filename = orig_input_filename;
2128 const char *two_pass_input = NULL;
2129 if (need_downscale) {
2130 FOREACH_STREAM(stream, streams) {
2131 if (stream->config.two_pass_input) {
2132 two_pass_input = stream->config.two_pass_input;
2133 input.filename = two_pass_input;
2134 break;
2135 }
2136 }
2137 }
2138
2139 open_input_file(&input, global.csp);
2140
2141 /* If the input file doesn't specify its w/h (raw files), try to get
2142 * the data from the first stream's configuration.
2143 */
2144 if (!input.width || !input.height) {
2145 if (two_pass_input) {
2146 FOREACH_STREAM(stream, streams) {
2147 if (stream->config.two_pass_width && stream->config.two_pass_height) {
2148 input.width = stream->config.two_pass_width;
2149 input.height = stream->config.two_pass_height;
2150 break;
2151 }
2152 }
2153 } else {
2154 FOREACH_STREAM(stream, streams) {
2155 if (stream->config.cfg.g_w && stream->config.cfg.g_h) {
2156 input.width = stream->config.cfg.g_w;
2157 input.height = stream->config.cfg.g_h;
2158 break;
2159 }
2160 }
2161 }
2162 }
2163
2164 /* Update stream configurations from the input file's parameters */
2165 if (!input.width || !input.height) {
2166 if (two_pass_input) {
2167 fatal(
2168 "Specify downscaled stream dimensions with --two-pass-width "
2169 " and --two-pass-height");
2170 } else {
2171 fatal(
2172 "Specify stream dimensions with --width (-w) "
2173 " and --height (-h)");
2174 }
2175 }
2176
2177 if (need_downscale) {
2178 FOREACH_STREAM(stream, streams) {
2179 if (stream->config.two_pass_width && stream->config.two_pass_height) {
2180 stream->config.cfg.g_w = stream->config.two_pass_width;
2181 stream->config.cfg.g_h = stream->config.two_pass_height;
2182 } else if (two_pass_input) {
2183 stream->config.cfg.g_w = input.width;
2184 stream->config.cfg.g_h = input.height;
2185 } else if (stream->orig_width && stream->orig_height) {
2186#if CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2187 stream->config.cfg.g_w = stream->orig_width;
2188 stream->config.cfg.g_h = stream->orig_height;
2189#else // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2190 stream->config.cfg.g_w = (stream->orig_width + 1) / 2;
2191 stream->config.cfg.g_h = (stream->orig_height + 1) / 2;
2192#endif // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2193 } else {
2194#if CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2195 stream->config.cfg.g_w = input.width;
2196 stream->config.cfg.g_h = input.height;
2197#else // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2198 stream->config.cfg.g_w = (input.width + 1) / 2;
2199 stream->config.cfg.g_h = (input.height + 1) / 2;
2200#endif // CONFIG_BITRATE_ACCURACY || CONFIG_BITRATE_ACCURACY_BL
2201 }
2202 }
2203 }
2204
2205 /* If input file does not specify bit-depth but input-bit-depth parameter
2206 * exists, assume that to be the input bit-depth. However, if the
2207 * input-bit-depth paramter does not exist, assume the input bit-depth
2208 * to be the same as the codec bit-depth.
2209 */
2210 if (!input.bit_depth) {
2211 FOREACH_STREAM(stream, streams) {
2212 if (stream->config.cfg.g_input_bit_depth)
2213 input.bit_depth = stream->config.cfg.g_input_bit_depth;
2214 else
2215 input.bit_depth = stream->config.cfg.g_input_bit_depth =
2216 (int)stream->config.cfg.g_bit_depth;
2217 }
2218 if (input.bit_depth > 8) input.fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
2219 } else {
2220 FOREACH_STREAM(stream, streams) {
2221 stream->config.cfg.g_input_bit_depth = input.bit_depth;
2222 }
2223 }
2224
2225 FOREACH_STREAM(stream, streams) {
2226 if (input.fmt != AOM_IMG_FMT_I420 && input.fmt != AOM_IMG_FMT_I42016 &&
2227 input.fmt != AOM_IMG_FMT_NV12) {
2228 /* Automatically upgrade if input is non-4:2:0 but a 4:2:0 profile
2229 was selected. */
2230 switch (stream->config.cfg.g_profile) {
2231 case 0:
2232 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2233 input.fmt == AOM_IMG_FMT_I44416)) {
2234 if (!stream->config.cfg.monochrome) {
2235 stream->config.cfg.g_profile = 1;
2236 profile_updated = 1;
2237 }
2238 } else if (input.bit_depth == 12 ||
2239 ((input.fmt == AOM_IMG_FMT_I422 ||
2240 input.fmt == AOM_IMG_FMT_I42216) &&
2241 !stream->config.cfg.monochrome)) {
2242 stream->config.cfg.g_profile = 2;
2243 profile_updated = 1;
2244 }
2245 break;
2246 case 1:
2247 if (input.bit_depth == 12 || input.fmt == AOM_IMG_FMT_I422 ||
2248 input.fmt == AOM_IMG_FMT_I42216) {
2249 stream->config.cfg.g_profile = 2;
2250 profile_updated = 1;
2251 } else if (input.bit_depth < 12 &&
2252 (input.fmt == AOM_IMG_FMT_I420 ||
2253 input.fmt == AOM_IMG_FMT_I42016)) {
2254 stream->config.cfg.g_profile = 0;
2255 profile_updated = 1;
2256 }
2257 break;
2258 case 2:
2259 if (input.bit_depth < 12 && (input.fmt == AOM_IMG_FMT_I444 ||
2260 input.fmt == AOM_IMG_FMT_I44416)) {
2261 stream->config.cfg.g_profile = 1;
2262 profile_updated = 1;
2263 } else if (input.bit_depth < 12 &&
2264 (input.fmt == AOM_IMG_FMT_I420 ||
2265 input.fmt == AOM_IMG_FMT_I42016)) {
2266 stream->config.cfg.g_profile = 0;
2267 profile_updated = 1;
2268 } else if (input.bit_depth == 12 &&
2269 input.file_type == FILE_TYPE_Y4M) {
2270 // Note that here the input file values for chroma subsampling
2271 // are used instead of those from the command line.
2272 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2274 input.y4m.dst_c_dec_h >> 1);
2275 ctx_exit_on_error(&stream->encoder,
2276 "Failed to set chroma subsampling x");
2277 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2279 input.y4m.dst_c_dec_v >> 1);
2280 ctx_exit_on_error(&stream->encoder,
2281 "Failed to set chroma subsampling y");
2282 } else if (input.bit_depth == 12 &&
2283 input.file_type == FILE_TYPE_RAW) {
2284 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2286 stream->chroma_subsampling_x);
2287 ctx_exit_on_error(&stream->encoder,
2288 "Failed to set chroma subsampling x");
2289 AOM_CODEC_CONTROL_TYPECHECKED(&stream->encoder,
2291 stream->chroma_subsampling_y);
2292 ctx_exit_on_error(&stream->encoder,
2293 "Failed to set chroma subsampling y");
2294 }
2295 break;
2296 default: break;
2297 }
2298 }
2299 /* Automatically set the codec bit depth to match the input bit depth.
2300 * Upgrade the profile if required. */
2301 if (stream->config.cfg.g_input_bit_depth >
2302 (unsigned int)stream->config.cfg.g_bit_depth) {
2303 stream->config.cfg.g_bit_depth = stream->config.cfg.g_input_bit_depth;
2304 if (!global.quiet) {
2305 fprintf(stderr,
2306 "Warning: automatically updating bit depth to %d to "
2307 "match input format.\n",
2308 stream->config.cfg.g_input_bit_depth);
2309 }
2310 }
2311#if !CONFIG_AV1_HIGHBITDEPTH
2312 if (stream->config.cfg.g_bit_depth > 8) {
2313 fatal("Unsupported bit-depth with CONFIG_AV1_HIGHBITDEPTH=0\n");
2314 }
2315#endif // CONFIG_AV1_HIGHBITDEPTH
2316 if (stream->config.cfg.g_bit_depth > 10) {
2317 switch (stream->config.cfg.g_profile) {
2318 case 0:
2319 case 1:
2320 stream->config.cfg.g_profile = 2;
2321 profile_updated = 1;
2322 break;
2323 default: break;
2324 }
2325 }
2326 if (stream->config.cfg.g_bit_depth > 8) {
2327 stream->config.use_16bit_internal = 1;
2328 }
2329 if (profile_updated && !global.quiet) {
2330 fprintf(stderr,
2331 "Warning: automatically updating to profile %d to "
2332 "match input format.\n",
2333 stream->config.cfg.g_profile);
2334 }
2335 if (global.show_psnr == 2 &&
2336 stream->config.cfg.g_input_bit_depth ==
2337 (unsigned int)stream->config.cfg.g_bit_depth) {
2338 fprintf(stderr,
2339 "Warning: --psnr==2 and --psnr==1 will provide same "
2340 "results when input bit-depth == stream bit-depth, "
2341 "falling back to default psnr value\n");
2342 global.show_psnr = 1;
2343 }
2344 if (global.show_psnr < 0 || global.show_psnr > 2) {
2345 fprintf(stderr,
2346 "Warning: --psnr can take only 0,1,2 as values,"
2347 "falling back to default psnr value\n");
2348 global.show_psnr = 1;
2349 }
2350 /* Set limit */
2351 stream->config.cfg.g_limit = global.limit;
2352 }
2353
2354 FOREACH_STREAM(stream, streams) {
2355 set_stream_dimensions(stream, input.width, input.height);
2356 stream->config.color_range = input.color_range;
2357 }
2358 FOREACH_STREAM(stream, streams) { validate_stream_config(stream, &global); }
2359
2360 /* Ensure that --passes and --pass are consistent. If --pass is set and
2361 * --passes >= 2, ensure --fpf was set.
2362 */
2363 if (global.pass > 0 && global.pass <= 3 && global.passes >= 2) {
2364 FOREACH_STREAM(stream, streams) {
2365 if (!stream->config.stats_fn)
2366 die("Stream %d: Must specify --fpf when --pass=%d"
2367 " and --passes=%d\n",
2368 stream->index, global.pass, global.passes);
2369 }
2370 }
2371
2372#if !CONFIG_WEBM_IO
2373 FOREACH_STREAM(stream, streams) {
2374 if (stream->config.write_webm) {
2375 stream->config.write_webm = 0;
2376 stream->config.write_ivf = 0;
2377 aom_tools_warn("aomenc compiled w/o WebM support. Writing OBU stream.");
2378 }
2379 }
2380#endif
2381
2382 /* Use the frame rate from the file only if none was specified
2383 * on the command-line.
2384 */
2385 if (!global.have_framerate) {
2386 global.framerate.num = input.framerate.numerator;
2387 global.framerate.den = input.framerate.denominator;
2388 }
2389 FOREACH_STREAM(stream, streams) {
2390 stream->config.cfg.g_timebase.den = global.framerate.num;
2391 stream->config.cfg.g_timebase.num = global.framerate.den;
2392 }
2393 /* Show configuration */
2394 if (global.verbose && pass == 0) {
2395 FOREACH_STREAM(stream, streams) {
2396 show_stream_config(stream, &global, &input);
2397 }
2398 }
2399
2400 if (pass == (global.pass ? global.pass - 1 : 0)) {
2401 // The Y4M reader does its own allocation.
2402 if (input.file_type != FILE_TYPE_Y4M) {
2403 aom_img_alloc(&raw, input.fmt, input.width, input.height, 32);
2404 }
2405 FOREACH_STREAM(stream, streams) {
2406 stream->rate_hist =
2407 init_rate_histogram(&stream->config.cfg, &global.framerate);
2408 }
2409 }
2410
2411 FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
2412 FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
2413 FOREACH_STREAM(stream, streams) {
2414 char *encoder_settings = NULL;
2415#if CONFIG_WEBM_IO
2416 // Test frameworks may compare outputs from different versions, but only
2417 // wish to check for bitstream changes. The encoder-settings tag, however,
2418 // can vary if the version is updated, even if no encoder algorithm
2419 // changes were made. To work around this issue, do not output
2420 // the encoder-settings tag when --debug is enabled (which is the flag
2421 // that test frameworks should use, when they want deterministic output
2422 // from the container format).
2423 if (stream->config.write_webm && !stream->webm_ctx.debug) {
2424 encoder_settings = extract_encoder_settings(
2425 aom_codec_version_str(), argv_, argc, input.filename);
2426 if (encoder_settings == NULL) {
2427 fprintf(
2428 stderr,
2429 "Warning: unable to extract encoder settings. Continuing...\n");
2430 }
2431 }
2432#endif
2433 open_output_file(stream, &global, &input.pixel_aspect_ratio,
2434 encoder_settings);
2435 free(encoder_settings);
2436 }
2437
2438 if (strcmp(get_short_name_by_aom_encoder(global.codec), "av1") == 0) {
2439 // Check to see if at least one stream uses 16 bit internal.
2440 // Currently assume that the bit_depths for all streams using
2441 // highbitdepth are the same.
2442 FOREACH_STREAM(stream, streams) {
2443 if (stream->config.use_16bit_internal) {
2444 do_16bit_internal = 1;
2445 }
2446 input_shift = (int)stream->config.cfg.g_bit_depth -
2447 stream->config.cfg.g_input_bit_depth;
2448 }
2449 }
2450
2451 frame_avail = 1;
2452 got_data = 0;
2453
2454 while (frame_avail || got_data) {
2455 struct aom_usec_timer timer;
2456
2457 if (!global.limit || frames_in < global.limit) {
2458 frame_avail = read_frame(&input, &raw);
2459
2460 if (frame_avail) frames_in++;
2461 seen_frames =
2462 frames_in > global.skip_frames ? frames_in - global.skip_frames : 0;
2463
2464 if (!global.quiet) {
2465 float fps = usec_to_fps(cx_time, seen_frames);
2466 fprintf(stderr, "\rPass %d/%d ", pass + 1, global.passes);
2467
2468 if (stream_cnt == 1)
2469 fprintf(stderr, "frame %4d/%-4d %7" PRId64 "B ", frames_in,
2470 streams->frames_out, (int64_t)streams->nbytes);
2471 else
2472 fprintf(stderr, "frame %4d ", frames_in);
2473
2474 fprintf(stderr, "%7" PRId64 " %s %.2f %s ",
2475 cx_time > 9999999 ? cx_time / 1000 : cx_time,
2476 cx_time > 9999999 ? "ms" : "us", fps >= 1.0 ? fps : fps * 60,
2477 fps >= 1.0 ? "fps" : "fpm");
2478 print_time("ETA", estimated_time_left);
2479 // mingw-w64 gcc does not match msvc for stderr buffering behavior
2480 // and uses line buffering, thus the progress output is not
2481 // real-time. The fflush() is here to make sure the progress output
2482 // is sent out while the clip is being processed.
2483 fflush(stderr);
2484 }
2485
2486 } else {
2487 frame_avail = 0;
2488 }
2489
2490 if (frames_in > global.skip_frames) {
2491 aom_image_t *frame_to_encode;
2492 if (input_shift || (do_16bit_internal && input.bit_depth == 8)) {
2493 assert(do_16bit_internal);
2494 // Input bit depth and stream bit depth do not match, so up
2495 // shift frame to stream bit depth
2496 if (!allocated_raw_shift) {
2497 aom_img_alloc(&raw_shift, raw.fmt | AOM_IMG_FMT_HIGHBITDEPTH,
2498 input.width, input.height, 32);
2499 allocated_raw_shift = 1;
2500 }
2501 aom_img_upshift(&raw_shift, &raw, input_shift);
2502 frame_to_encode = &raw_shift;
2503 } else {
2504 frame_to_encode = &raw;
2505 }
2506 aom_usec_timer_start(&timer);
2507 if (do_16bit_internal) {
2508 assert(frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH);
2509 FOREACH_STREAM(stream, streams) {
2510 if (stream->config.use_16bit_internal)
2511 encode_frame(stream, &global,
2512 frame_avail ? frame_to_encode : NULL, frames_in);
2513 else
2514 assert(0);
2515 }
2516 } else {
2517 assert((frame_to_encode->fmt & AOM_IMG_FMT_HIGHBITDEPTH) == 0);
2518 FOREACH_STREAM(stream, streams) {
2519 encode_frame(stream, &global, frame_avail ? frame_to_encode : NULL,
2520 frames_in);
2521 }
2522 }
2523 aom_usec_timer_mark(&timer);
2524 cx_time += aom_usec_timer_elapsed(&timer);
2525
2526 FOREACH_STREAM(stream, streams) { update_quantizer_histogram(stream); }
2527
2528 got_data = 0;
2529 FOREACH_STREAM(stream, streams) {
2530 get_cx_data(stream, &global, &got_data);
2531 }
2532
2533 if (!got_data && input.length && streams != NULL &&
2534 !streams->frames_out) {
2535 lagged_count = global.limit ? seen_frames : ftello(input.file);
2536 } else if (input.length) {
2537 int64_t remaining;
2538 int64_t rate;
2539
2540 if (global.limit) {
2541 const int64_t frame_in_lagged = (seen_frames - lagged_count) * 1000;
2542
2543 rate = cx_time ? frame_in_lagged * (int64_t)1000000 / cx_time : 0;
2544 remaining = 1000 * (global.limit - global.skip_frames -
2545 seen_frames + lagged_count);
2546 } else {
2547 const int64_t input_pos = ftello(input.file);
2548 const int64_t input_pos_lagged = input_pos - lagged_count;
2549 const int64_t input_limit = input.length;
2550
2551 rate = cx_time ? input_pos_lagged * (int64_t)1000000 / cx_time : 0;
2552 remaining = input_limit - input_pos + lagged_count;
2553 }
2554
2555 average_rate =
2556 (average_rate <= 0) ? rate : (average_rate * 7 + rate) / 8;
2557 estimated_time_left = average_rate ? remaining / average_rate : -1;
2558 }
2559
2560 if (got_data && global.test_decode != TEST_DECODE_OFF) {
2561 FOREACH_STREAM(stream, streams) {
2562 test_decode(stream, global.test_decode);
2563 }
2564 }
2565 }
2566
2567 fflush(stdout);
2568 if (!global.quiet) fprintf(stderr, "\033[K");
2569 }
2570
2571 if (stream_cnt > 1) fprintf(stderr, "\n");
2572
2573 if (!global.quiet) {
2574 FOREACH_STREAM(stream, streams) {
2575 const int64_t bpf =
2576 seen_frames ? (int64_t)(stream->nbytes * 8 / seen_frames) : 0;
2577 const int64_t bps = bpf * global.framerate.num / global.framerate.den;
2578 fprintf(stderr,
2579 "\rPass %d/%d frame %4d/%-4d %7" PRId64 "B %7" PRId64
2580 "b/f %7" PRId64
2581 "b/s"
2582 " %7" PRId64 " %s (%.2f fps)\033[K\n",
2583 pass + 1, global.passes, frames_in, stream->frames_out,
2584 (int64_t)stream->nbytes, bpf, bps,
2585 stream->cx_time > 9999999 ? stream->cx_time / 1000
2586 : stream->cx_time,
2587 stream->cx_time > 9999999 ? "ms" : "us",
2588 usec_to_fps(stream->cx_time, seen_frames));
2589 // This instance of cr does not need fflush as it is followed by a
2590 // newline in the same string.
2591 }
2592 }
2593
2594 if (global.show_psnr >= 1) {
2595 if (get_fourcc_by_aom_encoder(global.codec) == AV1_FOURCC) {
2596 FOREACH_STREAM(stream, streams) {
2597 int64_t bps = 0;
2598 if (global.show_psnr == 1) {
2599 if (stream->psnr_count[0] && seen_frames && global.framerate.den) {
2600 bps = (int64_t)stream->nbytes * 8 *
2601 (int64_t)global.framerate.num / global.framerate.den /
2602 seen_frames;
2603 }
2604 show_psnr(stream, (1 << stream->config.cfg.g_input_bit_depth) - 1,
2605 bps);
2606 }
2607 if (global.show_psnr == 2) {
2608#if CONFIG_AV1_HIGHBITDEPTH
2609 if (stream->config.cfg.g_input_bit_depth <
2610 (unsigned int)stream->config.cfg.g_bit_depth)
2611 show_psnr_hbd(stream, (1 << stream->config.cfg.g_bit_depth) - 1,
2612 bps);
2613#endif
2614 }
2615 }
2616 } else {
2617 FOREACH_STREAM(stream, streams) { show_psnr(stream, 255.0, 0); }
2618 }
2619 }
2620
2621 if (pass == global.passes - 1) {
2622 FOREACH_STREAM(stream, streams) {
2623 int num_operating_points;
2624 int levels[32];
2625 int target_levels[32];
2627 &num_operating_points);
2628 aom_codec_control(&stream->encoder, AV1E_GET_SEQ_LEVEL_IDX, levels);
2630 target_levels);
2631
2632 for (int i = 0; i < num_operating_points; i++) {
2633 if (levels[i] > target_levels[i]) {
2634 if (levels[i] == 31) {
2635 aom_tools_warn(
2636 "Failed to encode to target level %d.%d for operating point "
2637 "%d. The output level is SEQ_LEVEL_MAX",
2638 2 + (target_levels[i] >> 2), target_levels[i] & 3, i);
2639 } else {
2640 aom_tools_warn(
2641 "Failed to encode to target level %d.%d for operating point "
2642 "%d. The output level is %d.%d",
2643 2 + (target_levels[i] >> 2), target_levels[i] & 3, i,
2644 2 + (levels[i] >> 2), levels[i] & 3);
2645 }
2646 }
2647 }
2648 }
2649 }
2650
2651 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->encoder); }
2652
2653 if (global.test_decode != TEST_DECODE_OFF) {
2654 FOREACH_STREAM(stream, streams) { aom_codec_destroy(&stream->decoder); }
2655 }
2656
2657 close_input_file(&input);
2658
2659 if (global.test_decode == TEST_DECODE_FATAL) {
2660 FOREACH_STREAM(stream, streams) { res |= stream->mismatch_seen; }
2661 }
2662 FOREACH_STREAM(stream, streams) {
2663 close_output_file(stream, get_fourcc_by_aom_encoder(global.codec));
2664 }
2665
2666 FOREACH_STREAM(stream, streams) {
2667 stats_close(&stream->stats, global.passes - 1);
2668 }
2669
2670 if (global.pass) break;
2671 }
2672
2673 if (global.show_q_hist_buckets) {
2674 FOREACH_STREAM(stream, streams) {
2675 show_q_histogram(stream->counts, global.show_q_hist_buckets);
2676 }
2677 }
2678
2679 if (global.show_rate_hist_buckets) {
2680 FOREACH_STREAM(stream, streams) {
2681 show_rate_histogram(stream->rate_hist, &stream->config.cfg,
2682 global.show_rate_hist_buckets);
2683 }
2684 }
2685 FOREACH_STREAM(stream, streams) { destroy_rate_histogram(stream->rate_hist); }
2686
2687#if CONFIG_INTERNAL_STATS
2688 /* TODO(jkoleszar): This doesn't belong in this executable. Do it for now,
2689 * to match some existing utilities.
2690 */
2691 if (!(global.pass == 1 && global.passes == 2)) {
2692 FOREACH_STREAM(stream, streams) {
2693 FILE *f = fopen("opsnr.stt", "a");
2694 if (stream->mismatch_seen) {
2695 fprintf(f, "First mismatch occurred in frame %d\n",
2696 stream->mismatch_seen);
2697 } else {
2698 fprintf(f, "No mismatch detected in recon buffers\n");
2699 }
2700 fclose(f);
2701 }
2702 }
2703#endif
2704
2705 if (allocated_raw_shift) aom_img_free(&raw_shift);
2706 aom_img_free(&raw);
2707 free(argv);
2708 free(streams);
2709 return res ? EXIT_FAILURE : EXIT_SUCCESS;
2710}
Describes the decoder algorithm interface to applications.
Describes the encoder algorithm interface to applications.
#define MAX_TILE_WIDTHS
Maximum number of tile widths in tile widths array.
Definition aom_encoder.h:866
#define MAX_TILE_HEIGHTS
Maximum number of tile heights in tile heights array.
Definition aom_encoder.h:879
#define AOM_PLANE_U
Definition aom_image.h:227
@ AOM_CSP_UNKNOWN
Definition aom_image.h:143
enum aom_chroma_sample_position aom_chroma_sample_position_t
List of chroma sample positions.
#define AOM_PLANE_Y
Definition aom_image.h:226
#define AOM_PLANE_V
Definition aom_image.h:228
enum aom_color_range aom_color_range_t
List of supported color range.
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition aom_image.h:38
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
@ AOM_IMG_FMT_I42216
Definition aom_image.h:58
@ AOM_IMG_FMT_I42016
Definition aom_image.h:56
@ AOM_IMG_FMT_I444
Definition aom_image.h:50
@ AOM_IMG_FMT_I422
Definition aom_image.h:49
@ AOM_IMG_FMT_I44416
Definition aom_image.h:59
@ AOM_IMG_FMT_I420
Definition aom_image.h:45
@ AOM_IMG_FMT_NV12
Definition aom_image.h:54
@ AOM_IMG_FMT_YV12
Definition aom_image.h:43
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
Provides definitions for using AOM or AV1 within the aom Decoder interface.
@ AV1_SET_TILE_MODE
Codec control function to set the tile coding mode, unsigned int parameter.
Definition aomdx.h:316
@ AV1D_SET_IS_ANNEXB
Codec control function to indicate whether bitstream is in Annex-B format, unsigned int parameter.
Definition aomdx.h:352
@ AV1_SET_DECODE_TILE_ROW
Codec control function to set the range of tile decoding, int parameter.
Definition aomdx.h:307
@ AV1E_SET_MATRIX_COEFFICIENTS
Codec control function to set transfer function info, int parameter.
Definition aomcx.h:583
@ AV1E_SET_ENABLE_INTERINTER_WEDGE
Codec control function to turn on / off interinter wedge compound, int parameter.
Definition aomcx.h:1021
@ AV1E_SET_ENABLE_DIAGONAL_INTRA
Codec control function to turn on / off D45 to D203 intra mode usage, int parameter.
Definition aomcx.h:1360
@ AV1E_SET_MAX_GF_INTERVAL
Codec control function to set minimum interval between GF/ARF frames, unsigned int parameter.
Definition aomcx.h:604
@ AV1E_SET_ROW_MT
Codec control function to enable the row based multi-threading of the encoder, unsigned int parameter...
Definition aomcx.h:371
@ AV1E_SET_ENABLE_SMOOTH_INTRA
Codec control function to turn on / off smooth intra modes usage, int parameter.
Definition aomcx.h:1081
@ AOME_SET_SHARPNESS
Codec control function to set the sharpness parameter, unsigned int parameter.
Definition aomcx.h:247
@ AV1E_GET_TARGET_SEQ_LEVEL_IDX
Codec control function to get the target sequence level index for each operating point....
Definition aomcx.h:1467
@ AV1E_SET_RATE_DISTRIBUTION_INFO
Codec control to set the input file for rate distribution used in all intra mode, const char * parame...
Definition aomcx.h:1526
@ AV1E_SET_ENABLE_TPL_MODEL
Codec control function to enable RDO modulated by frame temporal dependency, unsigned int parameter.
Definition aomcx.h:418
@ AOME_GET_LAST_QUANTIZER_64
Codec control function to get last quantizer chosen by the encoder, int* parameter.
Definition aomcx.h:269
@ AV1E_SET_AQ_MODE
Codec control function to set adaptive quantization mode, unsigned int parameter.
Definition aomcx.h:478
@ AV1E_SET_REDUCED_REFERENCE_SET
Control to use reduced set of single and compound references, int parameter.
Definition aomcx.h:1237
@ AV1E_GET_NUM_OPERATING_POINTS
Codec control function to get the number of operating points. int* parameter.
Definition aomcx.h:1472
@ AV1E_SET_GF_MIN_PYRAMID_HEIGHT
Control to select minimum height for the GF group pyramid structure, unsigned int parameter.
Definition aomcx.h:1332
@ AV1E_SET_ENABLE_PAETH_INTRA
Codec control function to turn on / off Paeth intra mode usage, int parameter.
Definition aomcx.h:1089
@ AV1E_SET_TUNE_CONTENT
Codec control function to set content type, aom_tune_content parameter.
Definition aomcx.h:507
@ AV1E_SET_CDF_UPDATE_MODE
Codec control function to set CDF update mode, unsigned int parameter.
Definition aomcx.h:516
@ AV1E_SET_CHROMA_SUBSAMPLING_X
Sets the chroma subsampling x value, unsigned int parameter.
Definition aomcx.h:1200
@ AV1E_SET_COLOR_RANGE
Codec control function to set color range bit, int parameter.
Definition aomcx.h:616
@ AV1E_SET_ENABLE_RESTORATION
Codec control function to encode with Loop Restoration Filter, unsigned int parameter.
Definition aomcx.h:691
@ AV1E_SET_ENABLE_ANGLE_DELTA
Codec control function to turn on/off intra angle delta, int parameter.
Definition aomcx.h:1128
@ AV1E_SET_MIN_GF_INTERVAL
Codec control function to set minimum interval between GF/ARF frames, unsigned int parameter.
Definition aomcx.h:597
@ AOME_SET_ARNR_MAXFRAMES
Codec control function to set the max no of frames to create arf, unsigned int parameter.
Definition aomcx.h:274
@ AV1E_SET_MV_COST_UPD_FREQ
Control to set frequency of the cost updates for motion vectors, unsigned int parameter.
Definition aomcx.h:1267
@ AV1E_SET_INTRA_DEFAULT_TX_ONLY
Control to use default tx type only for intra modes, int parameter.
Definition aomcx.h:1216
@ AV1E_SET_TRANSFER_CHARACTERISTICS
Codec control function to set transfer function info, int parameter.
Definition aomcx.h:562
@ AV1E_SET_MTU
Codec control function to set an MTU size for a tile group, unsigned int parameter.
Definition aomcx.h:811
@ AV1E_SET_DISABLE_TRELLIS_QUANT
Codec control function to encode without trellis quantization, unsigned int parameter.
Definition aomcx.h:718
@ AV1E_SET_ENABLE_INTRABC
Codec control function to turn on/off intra block copy mode, int parameter.
Definition aomcx.h:1124
@ AV1E_SET_ENABLE_AB_PARTITIONS
Codec control function to enable/disable AB partitions, int parameter.
Definition aomcx.h:829
@ AV1E_SET_ENABLE_INTERINTRA_COMP
Codec control function to turn on / off interintra compound for a sequence, int parameter.
Definition aomcx.h:997
@ AV1E_SET_FILM_GRAIN_TEST_VECTOR
Codec control function to add film grain parameters (one of several preset types) info in the bitstre...
Definition aomcx.h:1186
@ AV1E_SET_ENABLE_CHROMA_DELTAQ
Codec control function to turn on / off delta quantization in chroma planes for a sequence,...
Definition aomcx.h:973
@ AV1E_SET_ENABLE_DUAL_FILTER
Codec control function to turn on / off dual interpolation filter for a sequence, int parameter.
Definition aomcx.h:965
@ AV1E_SET_FRAME_PARALLEL_DECODING
Codec control function to enable frame parallel decoding feature, unsigned int parameter.
Definition aomcx.h:441
@ AV1E_SET_MIN_PARTITION_SIZE
Codec control function to set min partition size, int parameter.
Definition aomcx.h:848
@ AV1E_SET_ENABLE_WARPED_MOTION
Codec control function to turn on / off warped motion usage at sequence level, int parameter.
Definition aomcx.h:1049
@ AV1E_SET_FORCE_VIDEO_MODE
Codec control function to force video mode, unsigned int parameter.
Definition aomcx.h:698
@ AV1E_SET_CHROMA_SUBSAMPLING_Y
Sets the chroma subsampling y value, unsigned int parameter.
Definition aomcx.h:1203
@ AV1E_SET_ENABLE_INTRA_EDGE_FILTER
Codec control function to turn on / off intra edge filter at sequence level, int parameter.
Definition aomcx.h:867
@ AV1E_SET_COEFF_COST_UPD_FREQ
Control to set frequency of the cost updates for coefficients, unsigned int parameter.
Definition aomcx.h:1247
@ AV1E_SET_ENABLE_DIRECTIONAL_INTRA
Codec control function to turn on / off directional intra mode usage, int parameter.
Definition aomcx.h:1389
@ AV1E_SET_MAX_INTER_BITRATE_PCT
Codec control function to set max data rate for inter frames, unsigned int parameter.
Definition aomcx.h:335
@ AV1E_SET_DENOISE_NOISE_LEVEL
Sets the noise level, int parameter.
Definition aomcx.h:1194
@ AV1E_SET_INTRA_DCT_ONLY
Control to use dct only for intra modes, int parameter.
Definition aomcx.h:1209
@ AV1E_SET_TILE_ROWS
Codec control function to set number of tile rows, unsigned int parameter.
Definition aomcx.h:408
@ AV1E_SET_ENABLE_REF_FRAME_MVS
Codec control function to turn on / off ref frame mvs (mfmv) usage at sequence level,...
Definition aomcx.h:946
@ AV1E_SET_FP_MT
Codec control function to enable frame parallel multi-threading of the encoder, unsigned int paramete...
Definition aomcx.h:1447
@ AV1E_SET_ENABLE_MASKED_COMP
Codec control function to turn on / off masked compound usage (wedge and diff-wtd compound modes) for...
Definition aomcx.h:981
@ AV1E_SET_VBR_CORPUS_COMPLEXITY_LAP
Control to set average complexity of the corpus in the case of single pass vbr based on LAP,...
Definition aomcx.h:1337
@ AV1E_SET_GF_MAX_PYRAMID_HEIGHT
Control to select maximum height for the GF group pyramid structure, unsigned int parameter.
Definition aomcx.h:1226
@ AV1E_SET_ENABLE_CDEF
Codec control function to encode with CDEF, unsigned int parameter.
Definition aomcx.h:681
@ AV1E_SET_ENABLE_FLIP_IDTX
Codec control function to turn on / off flip and identity transforms, int parameter.
Definition aomcx.h:911
@ AV1E_GET_SEQ_LEVEL_IDX
Codec control function to get sequence level index for each operating point. int* parameter....
Definition aomcx.h:653
@ AV1E_SET_FRAME_PERIODIC_BOOST
Codec control function to enable/disable periodic Q boost, unsigned int parameter.
Definition aomcx.h:490
@ AV1E_SET_DV_COST_UPD_FREQ
Control to set frequency of the cost updates for intrabc motion vectors, unsigned int parameter.
Definition aomcx.h:1370
@ AV1E_SET_AUTO_INTRA_TOOLS_OFF
Codec control to automatically turn off several intra coding tools, unsigned int parameter.
Definition aomcx.h:1431
@ AV1E_SET_ENABLE_RECT_TX
Codec control function to turn on / off rectangular transforms, int parameter.
Definition aomcx.h:923
@ AV1E_SET_ENABLE_DIST_WTD_COMP
Codec control function to turn on / off dist-wtd compound mode at sequence level, int parameter.
Definition aomcx.h:935
@ AV1E_SET_TIMING_INFO_TYPE
Codec control function to signal picture timing info in the bitstream, aom_timing_info_type_t paramet...
Definition aomcx.h:1179
@ AV1E_SET_SUPERBLOCK_SIZE
Codec control function to set intended superblock size, unsigned int parameter.
Definition aomcx.h:661
@ AV1E_SET_TIER_MASK
Control to set bit mask that specifies which tier each of the 32 possible operating points conforms t...
Definition aomcx.h:1275
@ AV1E_SET_ENABLE_INTERINTRA_WEDGE
Codec control function to turn on / off interintra wedge compound, int parameter.
Definition aomcx.h:1029
@ AV1E_SET_NOISE_SENSITIVITY
Codec control function to set noise sensitivity, unsigned int parameter.
Definition aomcx.h:498
@ AV1E_SET_ENABLE_DIFF_WTD_COMP
Codec control function to turn on / off difference weighted compound, int parameter.
Definition aomcx.h:1013
@ AV1E_SET_QUANT_B_ADAPT
Control to use adaptive quantize_b, int parameter.
Definition aomcx.h:1219
@ AV1E_SET_ENABLE_FILTER_INTRA
Codec control function to turn on / off filter intra usage at sequence level, int parameter.
Definition aomcx.h:1070
@ AV1E_SET_ENABLE_PALETTE
Codec control function to turn on/off palette mode, int parameter.
Definition aomcx.h:1120
@ AV1E_SET_ENABLE_CFL_INTRA
Codec control function to turn on / off CFL uv intra mode usage, int parameter.
Definition aomcx.h:1099
@ AV1E_SET_ENABLE_KEYFRAME_FILTERING
Codec control function to enable temporal filtering on key frame, unsigned int parameter.
Definition aomcx.h:427
@ AV1E_SET_NUM_TG
Codec control function to set a maximum number of tile groups, unsigned int parameter.
Definition aomcx.h:800
@ AOME_SET_MAX_INTRA_BITRATE_PCT
Codec control function to set max data rate for intra frames, unsigned int parameter.
Definition aomcx.h:312
@ AV1E_SET_ERROR_RESILIENT_MODE
Codec control function to enable error_resilient_mode, int parameter.
Definition aomcx.h:452
@ AV1E_SET_ENABLE_SMOOTH_INTERINTRA
Codec control function to turn on / off smooth inter-intra mode for a sequence, int parameter.
Definition aomcx.h:1005
@ AOME_SET_STATIC_THRESHOLD
Codec control function to set the threshold for MBs treated static, unsigned int parameter.
Definition aomcx.h:252
@ AV1E_SET_ENABLE_OBMC
Codec control function to predict with OBMC mode, unsigned int parameter.
Definition aomcx.h:708
@ AV1E_SET_PARTITION_INFO_PATH
Codec control to set the path for partition stats read and write. const char * parameter.
Definition aomcx.h:1375
@ AV1E_SET_ENABLE_LOW_COMPLEXITY_DECODE
Codec control to enable the low complexity decode mode, unsigned int parameter. Value of zero means t...
Definition aomcx.h:1588
@ AV1E_SET_MAX_PARTITION_SIZE
Codec control function to set max partition size, int parameter.
Definition aomcx.h:859
@ AV1E_SET_ENABLE_1TO4_PARTITIONS
Codec control function to enable/disable 1:4 and 4:1 partitions, int parameter.
Definition aomcx.h:837
@ AV1E_SET_DELTALF_MODE
Codec control function to turn on/off loopfilter modulation when delta q modulation is enabled,...
Definition aomcx.h:1152
@ AV1E_SET_ENABLE_TX64
Codec control function to turn on / off 64-length transforms, int parameter.
Definition aomcx.h:887
@ AOME_SET_TUNING
Codec control function to set visual tuning, aom_tune_metric (int) parameter.
Definition aomcx.h:288
@ AV1E_SET_TARGET_SEQ_LEVEL_IDX
Control to set target sequence level index for a certain operating point (OP), int parameter Possible...
Definition aomcx.h:646
@ AV1E_SET_CHROMA_SAMPLE_POSITION
Codec control function to set chroma 4:2:0 sample position info, aom_chroma_sample_position_t paramet...
Definition aomcx.h:590
@ AV1E_SET_REDUCED_TX_TYPE_SET
Control to use a reduced tx type set, int parameter.
Definition aomcx.h:1206
@ AV1E_SET_DELTAQ_STRENGTH
Set –deltaq-mode strength.
Definition aomcx.h:1410
@ AV1E_SET_INTER_DCT_ONLY
Control to use dct only for inter modes, int parameter.
Definition aomcx.h:1212
@ AV1E_SET_LOOPFILTER_CONTROL
Codec control to control loop filter.
Definition aomcx.h:1419
@ AOME_SET_ENABLEAUTOALTREF
Codec control function to enable automatic set and use alf frames, unsigned int parameter.
Definition aomcx.h:228
@ AV1E_ENABLE_RATE_GUIDE_DELTAQ
Codec control to enable the rate distribution guided delta quantization in all intra mode,...
Definition aomcx.h:1514
@ AV1E_SET_TILE_COLUMNS
Codec control function to set number of tile columns. unsigned int parameter.
Definition aomcx.h:390
@ AV1E_SET_ENABLE_ORDER_HINT
Codec control function to turn on / off frame order hint (int parameter). Affects: joint compound mod...
Definition aomcx.h:876
@ AV1E_SET_DELTAQ_MODE
Codec control function to set the delta q mode, unsigned int parameter.
Definition aomcx.h:1144
@ AV1E_SET_ENABLE_GLOBAL_MOTION
Codec control function to turn on / off global motion usage for a sequence, int parameter.
Definition aomcx.h:1039
@ AV1E_SET_FILM_GRAIN_TABLE
Codec control function to set the path to the film grain parameters, const char* parameter.
Definition aomcx.h:1191
@ AV1E_SET_QM_MAX
Codec control function to set the max quant matrix flatness, unsigned int parameter.
Definition aomcx.h:754
@ AV1E_SET_MAX_REFERENCE_FRAMES
Control to select maximum reference frames allowed per frame, int parameter.
Definition aomcx.h:1233
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition aomcx.h:220
@ AV1E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode, unsigned int parameter.
Definition aomcx.h:349
@ AV1E_SET_ENABLE_ONESIDED_COMP
Codec control function to turn on / off one sided compound usage for a sequence, int parameter.
Definition aomcx.h:989
@ AV1E_SET_DENOISE_BLOCK_SIZE
Sets the denoisers block size, unsigned int parameter.
Definition aomcx.h:1197
@ AV1E_SET_VMAF_MODEL_PATH
Codec control function to set the path to the VMAF model used when tuning the encoder for VMAF,...
Definition aomcx.h:1304
@ AV1E_SET_QM_MIN
Codec control function to set the min quant matrix flatness, unsigned int parameter.
Definition aomcx.h:742
@ AV1E_SET_ENABLE_QM
Codec control function to encode with quantisation matrices, unsigned int parameter.
Definition aomcx.h:729
@ AV1E_SET_ENABLE_OVERLAY
Codec control function to turn on / off overlay frames for filtered ALTREF frames,...
Definition aomcx.h:1117
@ AV1E_SET_ENABLE_RECT_PARTITIONS
Codec control function to enable/disable rectangular partitions, int parameter.
Definition aomcx.h:821
@ AV1E_SET_COLOR_PRIMARIES
Codec control function to set color space info, int parameter.
Definition aomcx.h:537
@ AOME_SET_CQ_LEVEL
Codec control function to set constrained / constant quality level, unsigned int parameter.
Definition aomcx.h:298
@ AV1E_SET_ENABLE_TX_SIZE_SEARCH
Control to turn on / off transform size search. Note: it can not work with non RD pick mode in real-t...
Definition aomcx.h:1399
@ AV1E_SET_MODE_COST_UPD_FREQ
Control to set frequency of the cost updates for mode, unsigned int parameter.
Definition aomcx.h:1257
@ AV1E_SET_MIN_CR
Control to set minimum compression ratio, unsigned int parameter Take integer values....
Definition aomcx.h:1282
@ AV1E_SET_LOSSLESS
Codec control function to set lossless encoding mode, unsigned int parameter.
Definition aomcx.h:363
@ AOME_SET_ARNR_STRENGTH
Codec control function to set the filter strength for the arf, unsigned int parameter.
Definition aomcx.h:279
@ AV1_GET_NEW_FRAME_IMAGE
Codec control function to get a pointer to the new frame.
Definition aom.h:70
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:271
const char * aom_codec_version_str(void)
Return the version information (as a string)
aom_codec_err_t aom_codec_set_option(aom_codec_ctx_t *ctx, const char *name, const char *value)
Key & Value API.
int64_t aom_codec_pts_t
Time Stamp Type.
Definition aom_codec.h:252
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
const char * aom_codec_err_to_string(aom_codec_err_t err)
Convert error number to printable string.
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
#define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data)
aom_codec_control wrapper macro (adds type-checking, less flexible)
Definition aom_codec.h:542
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:305
@ AOM_BITS_8
Definition aom_codec.h:336
aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data, size_t data_sz, void *user_priv)
Decode data.
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition aom_decoder.h:129
#define AOM_USAGE_GOOD_QUALITY
usage parameter analogous to AV1 GOOD QUALITY mode.
Definition aom_encoder.h:1014
#define AOM_USAGE_ALL_INTRA
usage parameter analogous to AV1 all intra mode.
Definition aom_encoder.h:1018
const aom_codec_cx_pkt_t * aom_codec_get_cx_data(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Encoded data iterator.
aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned long duration, aom_enc_frame_flags_t flags)
Encode a frame.
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition aom_encoder.h:943
aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int usage)
Get the default configuration for a usage.
#define AOM_USAGE_REALTIME
usage parameter analogous to AV1 REALTIME mode.
Definition aom_encoder.h:1016
#define AOM_CODEC_USE_HIGHBITDEPTH
Definition aom_encoder.h:80
#define AOM_CODEC_USE_PSNR
Initialization-time Feature Enabling.
Definition aom_encoder.h:79
@ AOM_RC_ONE_PASS
Definition aom_encoder.h:177
@ AOM_RC_SECOND_PASS
Definition aom_encoder.h:179
@ AOM_RC_THIRD_PASS
Definition aom_encoder.h:180
@ AOM_RC_FIRST_PASS
Definition aom_encoder.h:178
@ AOM_KF_DISABLED
Definition aom_encoder.h:203
@ AOM_CODEC_PSNR_PKT
Definition aom_encoder.h:113
@ AOM_CODEC_CX_FRAME_PKT
Definition aom_encoder.h:110
@ AOM_CODEC_STATS_PKT
Definition aom_encoder.h:111
Codec context structure.
Definition aom_codec.h:315
Encoder output packet.
Definition aom_encoder.h:122
size_t sz
Definition aom_encoder.h:127
enum aom_codec_cx_pkt_kind kind
Definition aom_encoder.h:123
double psnr[4]
Definition aom_encoder.h:145
aom_fixed_buf_t twopass_stats
Definition aom_encoder.h:140
aom_fixed_buf_t raw
Definition aom_encoder.h:156
union aom_codec_cx_pkt::@1 data
aom_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition aom_encoder.h:129
struct aom_codec_cx_pkt::@1::@2 frame
int partition_id
the partition id defines the decoding order of the partitions. Only applicable when "output partition...
Definition aom_encoder.h:136
void * buf
Definition aom_encoder.h:126
Initialization Configurations.
Definition aom_decoder.h:91
Encoder configuration structure.
Definition aom_encoder.h:387
struct aom_rational g_timebase
Stream timebase units.
Definition aom_encoder.h:489
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition aom_encoder.h:504
size_t sz
Definition aom_encoder.h:90
void * buf
Definition aom_encoder.h:89
Image Descriptor.
Definition aom_image.h:198
aom_chroma_sample_position_t csp
Definition aom_image.h:204
unsigned int y_chroma_shift
Definition aom_image.h:222
aom_img_fmt_t fmt
Definition aom_image.h:199
int stride[3]
Definition aom_image.h:232
unsigned char * img_data
Definition aom_image.h:246
unsigned int x_chroma_shift
Definition aom_image.h:221
unsigned int d_w
Definition aom_image.h:213
int bps
Definition aom_image.h:235
int monochrome
Definition aom_image.h:203
unsigned int d_h
Definition aom_image.h:214
unsigned char * planes[3]
Definition aom_image.h:231
int img_data_owner
Definition aom_image.h:247
int self_allocd
Definition aom_image.h:248
size_t sz
Definition aom_image.h:233
Rational Number.
Definition aom_encoder.h:164
int num
Definition aom_encoder.h:165
int den
Definition aom_encoder.h:166
Encoder Config Options.
Definition aom_encoder.h:227
unsigned int min_partition_size
min partition size 8, 16, 32, 64, 128
Definition aom_encoder.h:243
unsigned int max_partition_size
max partition size 8, 16, 32, 64, 128
Definition aom_encoder.h:239
unsigned int disable_trellis_quant
disable trellis quantization
Definition aom_encoder.h:355
unsigned int super_block_size
Superblock size 0, 64 or 128.
Definition aom_encoder.h:235